0

I am trying to set an image as background but I am not sure what is wrong with this code itself.. Could you please assist here and pointed out what can be changed? When I run it, it gives me a blank background as before without expected result.

public
    class MenuFrame extends JFrame {

    public MenuFrame(){
        generateMenuFrame();
    }

    public void generateMenuFrame()
    {
        JLabel background;
        JFrame menuFrame = new JFrame("Koronawirus AntiPlague - Menu");
        try {
            menuFrame.setIconImage(ImageIO.read(MenuFrame.class.getResourceAsStream("resources/images/Icon.png")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        ImageIcon backgroundImage = new ImageIcon("resources/images/MenuBackground.png");
        background = new JLabel("",backgroundImage,JLabel.CENTER);
        background.setBounds(0,0,getWidth(),getHeight());
        menuFrame.add(background);
        menuFrame.setSize(700,400);
        menuFrame.setVisible(true);
        menuFrame.setResizable(false);
        menuFrame.setLocationRelativeTo(null);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

UPDATE:

Thank you guys for any tips. As Andrew recommended I decided to stay with one instance without extands and moreover I used an override of the paintComponent method:

public
    class MenuFrame {

    private JPanel contentPane;

    public MenuFrame(){
        generateMenuFrame();
    }

    public void generateMenuFrame()
    {
        JFrame menuFrame = new JFrame("Koronawirus AntiPlague - Menu");
        try {
            menuFrame.setIconImage(ImageIO.read(MenuFrame.class.getResourceAsStream("resources/images/Icon.png")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        menuFrame.setSize(700,400);
        menuFrame.setVisible(true);
        menuFrame.setResizable(false);
        menuFrame.setLocationRelativeTo(null);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel() {
                public void paintComponent(Graphics g) {
                                      Image img = Toolkit.getDefaultToolkit().getImage(
                                      MenuFrame.class.getResource("resources/images/MenuBackground.jpg"));
                                      g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
                                     }
               };
        menuFrame.setContentPane(contentPane);
    }
}
matri1395
  • 1
  • 2
  • 2
    1) For starters, the BG image needs to be loaded as an application resource, as the code does with the icon. 2) The code both extends frame and creates an instance of one. Drop the first, stick with the 2nd. – Andrew Thompson May 18 '21 at 17:05
  • 1
    There's no need to `extends JFrame` since you're not changing any of its behavior. Take [this answer](https://stackoverflow.com/a/42446498/2180785) as a starting point – Frakcool May 18 '21 at 17:10

1 Answers1

0
background.setBounds(0,0,getWidth(),getHeight());

The above code will set the size of the label to (0, 0) since you are extending JFrame and invoking the getWidth() and getHeight() methods of the frame. The default size of all Swing components is (0, 0), so there is nothing to paint.

Don't attempt to play with the bounds of the label.

The size of the label will automatically be set to the size of the image.

You should then use:

//menuFrame.setSize(700,400);
menuFrame.pack();

so the frame can be size to properly display the label and its image.

camickr
  • 321,443
  • 19
  • 166
  • 288