0
public class main extends JFrame {
    JPanel panel = new JPanel();
    JButton playGame = new JButton("PLAY GAME");
    public void paint(java.awt.Graphics g) {
        super.paint(g);
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("./src/Images/menu.jpg"));
        } catch (IOException e) {

            e.printStackTrace();
        }
        g.drawImage(image, 0, 0, 1000, 600, this);
    }

    public main(){
        super();

        playGame.setBounds(390, 250, 220, 30);
        //panel.setBounds(80, 800, 200, 100);
        panel.setLayout(null);
        panel.add(playGame);
        add(panel);

        setTitle("MENU");
        setSize(1000,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        //setLayout(new FlowLayout());
        setVisible(true);
    }

    public static void main(String[] args) {
        new main();
}
}

I am trying to add my JButton over the image but it is coming behind the image.

The problem is I don't know how to add a background picture so that the buttons appear at the top of the picture. Is there any for me set up a background picture so that the other panels show up on top as well?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `panel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 16 '14 at 09:07

1 Answers1

0

Try to add you background image to your panel, and don't load your image from src folder because after compiling your classes will be created in bin folder, so use class.getResource() which gives you relative url to your image; another thing you should never override JFrame paint method because painting on JFrame is applied also over frame border, so try always to paint on a panel inside your frame by overriding paintComponent method of JPanel class:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class main extends JFrame
{
    JButton playGame = new JButton("PLAY GAME");
    JPanel panel = new JPanel()
    {
        public void paintComponent(java.awt.Graphics g)
        {
            super.paintComponent(g);
            BufferedImage image = null;
            try
            {
                image = ImageIO.read(main.class.getResource("/Images/menu.jpg"));
            }
            catch (IOException e)
            {

                e.printStackTrace();
            }
            g.drawImage(image, 0, 0, 1000, 600, this);
        }
    };

    public main()
    {
        super();

        playGame.setBounds(390, 250, 220, 30);
        // panel.setBounds(80, 800, 200, 100);
        panel.setLayout(null);
        panel.add(playGame);
        add(panel);

        setTitle("MENU");
        setSize(1000, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        // setLayout(new FlowLayout());
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new main();
    }
}
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28