1

I have a JFrame and the JPanel is inside it. My game's background is completely black as the entities are on top of it. I would just like to change the background into an image from my computer the "simplest" way without having to change much.

public Game() {
    // Frame
    JFrame container = new JFrame("Space Invaders");

    // Resolution
    JPanel panel = (JPanel) container.getContentPane();
    panel.setPreferredSize(new Dimension(800,600));
    panel.setLayout(null);

    // Canvas Size
    panel.setBounds(0,0,800,600);
    panel.add(this);

    // Window Visible
    container.pack();
    container.setResizable(false);
    container.setVisible(true);

    container.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Ryan
  • 49
  • 1
  • 3
  • 10
  • Create a `JLabel`, set it's layout to `BorderLayout`, apply the image to it like you would any other label, set this as the `contentPane` of the frame and continue as you have been. Of course, you could simply use `Graphics#drawImage`, but since I don't know how your code actually works, it's difficult to make a recommendation on what is "simplest" – MadProgrammer Nov 14 '15 at 05:37
  • Try whats suggested here too http://stackoverflow.com/questions/19125707/simplest-way-to-set-image-as-jpanel-background – coderrick Nov 14 '15 at 05:40

1 Answers1

4

You could...

Use a JLabel as the background component...

JFrame container = new JFrame("Space Invaders");
JLabel label = new JLabel(new ImageIcon(ImageIO.read(...)));
label.setLayout(new BorderLayout());
container.setContentPane(label);

// Resolution
// There are simply so many different ways to achieve this
// that are better it's not funny
//JPanel panel = (JPanel) container.getContentPane();
//panel.setPreferredSize(new Dimension(800, 600));
//panel.setLayout(null);

// Canvas Size
//setBounds(0, 0, 800, 600);
//panel.add(this);

// Window Visible
container.pack();
container.setResizable(false);
container.setVisible(true);

container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Pointless
//container.addWindowListener(new WindowAdapter() {
//    public void windowClosing(WindowEvent e) {
//          System.exit(0);
//      }
//});

The issue with this is:

  1. The label will only use the icon and text property to calculate the preferredSize of the label
  2. If you're using a java.awt.Canvas, then it will paint over this component, as java.awt.Canvas can't be transparent.

See Reading/Loading an Image and How to Use Labels for more details

You could...

Use Graphics#drawImage to draw the background image before you renderer the reset of your content.

This will be, generally, a better solution, as you gain complete control over the size and position of the image and will work if you're using java.awt.Canvas and a BufferStrategy

See 2D Graphics and Working with Images for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366