1

i've gone about setting the background Image of my JFrame but now my components are (I assume) stuck behind it. I've tried reading about top level containers but I just can't get my head around it T_T. Any one have any ideas ? Maybe I need to find another way to set the background image ? Thank you for any help. :

@SuppressWarnings("serial")
public class CopyOfMainMenu extends JFrame {

//Running the GUI
    public static void main(String[] args) throws IOException {
        CopyOfMainMenu gui2 = new CopyOfMainMenu();
        gui2.mainPanel();
    }

    public void thepanel () throws IOException {
        // GridBagLayout/Constraint
        GridBagConstraints gridbagc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new GridBagLayout());
        // Creating JButtons/Icons
        panel.add(buttonTwo, gbc); //SCOREBOARD
        panel.add(buttonThree, gbc); //INSTRUCTIONS
        // JButton size's
        button.setPreferredSize(new Dimension(400, 35))
        buttonTwo.setContentAreaFilled(false);
        buttonThree.setBorder(BorderFactory.createEmptyBorder());
        buttonThree.setContentAreaFilled(false);
    }
}

Again, thanks in advance for any help.

bgigurtsis
  • 145
  • 1
  • 2
  • 15
  • 2
    see http://stackoverflow.com/questions/11113159/background-image-in-a-nested-jpanel – Łukasz Rzeszotarski Sep 14 '13 at 19:58
  • 1
    Please go through this [thread](http://stackoverflow.com/q/9864267/1057230), which explains how to add images to the project. The last link, in the provided link, will explain clearly, if you doing it manually without using any IDE :-) – nIcE cOw Sep 17 '13 at 10:53

2 Answers2

3

You never add the panel anywhere (if you did, it would paint over the image). However, it's possible to use a JLabel as the container:

JComponent panel = new JLabel(new ImageIcon(ImageIO.read(new File("res/FinalBG.png"))));
panel.setLayout(new GridBagLayout());
...
// continue adding the components as before
...
frame.add(panel);

(or set the label as the content pane, which works just as well).

kiheru
  • 6,588
  • 25
  • 31
1

Inside the JFrame, override the paint method and draw your image using the Graphics object:

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(image, width, height, null);
}
anshrpr
  • 43
  • 8