In my Independent Study class, I'm working on making a 2d scrolling platform game with my classmate. We're just starting out on this project and we think it'll be a huge help with learning to use the paint methods and basic game design, but we're already running into a bit of an issue. We've created a JFrame and set the background to a static, non-changing image, and we're trying to create a foreground JPanel which will contain the scrolling "platforms" which will be what our character will jump on. In order to do this, I believe we will need to make everything on the jpanel transparent, apart from the paitnComponent contents (which will be the platforms, I think). If we don't do this, wouldn't the jpanel show up as a white box and block the background image, or am I mistaken on that? Anyway, the code is below. Is there any way to have a transparent jpanel, but have the paintComponent contents still show up? Currently, all that I can see when I run the program is the background, with no black rectangle at the top.
P.S. sorry if the formatting is a little whacked out. Transferring the code from my school computer to my home computer on google docs was a terrible idea.
import java.awt.*; import javax.swing.*; import javax.imageio.*; import java.io.*; class BackgroundTesting extends JFrame { BackgroundTesting() { //Main JFrame, contains the BG and setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //JPanel "JPan" setResizable(false); setSize(900, 447); setLocationRelativeTo(null); JPanel panel = new JPan(); add(panel); setVisible(true); } public void paint(Graphics g) { Image img; try { img = ImageIO.read(new File("/Users/java/Desktop/BG.png")); //Sets image "BG" to the g.drawImage(img, 0, 0, null); //Background of the JFrame } catch(Exception e) {} } } class JPan extends JPanel { JPan() { setOpaque(false); setSize(900, 447); setVisible(true); } public void paintComponent(Graphics g) { //Will contain the "scrolling" g.fillRect(50, 50, 25, 25); //Platforms } } class Frame { public static void main(String[] args) { BackgroundTesting bgTest = new BackgroundTesting(); } }