PNC
PNC

Reputation: 357

JPanel adding background

How to resize the inputpanel and gamepanel so it can take the whole frame? the inputpanel.setsize() wont work and the other problem is that the gamepanel wont display its background image.

what I did is I added a mainpanel to the frame that contains the other 2 panel the inputpanel and the gamepanel. Both panels displays with their borders but the size is very small and I needed them to fit the screen

public class GameMaster   {

            JFrame frame = new JFrame();
            JPanel gamepanel  = new Gamepanel();
            JPanel mainpanel = new JPanel();
            JPanel inputpanel = new JPanel();
            JButton button1 = new JButton("Hoy!");

            public GameMaster(){

                frame.setTitle("gayEdward vs Charlie's angels");
                frame.setSize(800,600);

                frame.setVisible(true);
                frame.setDefaultCloseOperation(3);
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new FlowLayout());


                gamepanel.setSize(600, 600);
                inputpanel.setSize(200, 600);
                mainpanel.setSize(800, 600);
                gamepanel.setBorder(BorderFactory.createLineBorder(Color.black));
                inputpanel.setBorder(BorderFactory.createLineBorder(Color.black));


                mainpanel.setLayout(new FlowLayout());




                mainpanel.add(gamepanel);
                mainpanel.add(inputpanel);


                frame.add(mainpanel);

            }

This is the Gamepanel class where The Gamepanel should set the image as its background

    public class Gamepanel extends JPanel {
        Image image;

        public Gamepanel(){

             try
                {
                  image = ImageIO.read(getClass().getResourceAsStream("C:\\Users\\phg00159\\workspace\\Mp2\\pvzbg.jpg"));;
                }
                catch (Exception e) { /*handled in paintComponent()*/ }

        }


          protected void paintComponent(Graphics g)
          {
            super.paintComponent(g); 
            if (image != null)
              g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
          }
    }

Upvotes: 0

Views: 124

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209102

You need to override getPreferredSize() in the GamePanel class. You should always do this when doing custom painting. The panel has no preferred size because it has no components. And the layout you are using FlowLayout takes the preferred size into consideration. Then just pack() the frame, instead of setting the size. Also note that setSize() for components is generally used for null layouts (which I strongly am against). With layout managers, they are determine by preferredSize

public class GamePanel extends JPanel {
    private Image image;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(this), image.getHeight(this));
        // or whatever you want the size to be
    }
}

Aside:

  • /*handled in paintComponent()*/ - A null check isn't a substitute for an empty catch block.

  • frame.setVisible() should the last thing you do after adding all your components


UPDATE

As @MadProgrammer pointed out to me, you want the panel to resize with the frame. In order to do that, you need to use a different layout manager. The two that won't respect the preferred size of your panels will be either GridLayout or BorderLayout. These will "stretch your components to fit". So choose one of them, or a combination of both, seeing as you have nested panels. But you should also keep the overriden getPreferredSize() and still pack() your frame This will give you the initial size of the frame, based on your preferred size

Upvotes: 2

Related Questions