0

I have got this piece of code:

frame.setTitle("SideScroller");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dim = new Dimension(1000, 500);
frame.setContentPane(gamePanel);               //gamePanel is an extended JPanel
gamePanel.setPreferredSize(dim);
frame.pack();  

After I run this code and fill the gamePanel with parts of 50x50p, which should fill the gamePanel and thus the frame completely, there are some empty rows of pixels at the right and bottom of the screen.

frame.getSize();    //gives us: 1016 x 539
frame.getInsets();  //gives: Top: 31 | Left: 8 | Bottom: 8 | Right: 8  

An image of the result:

JFrame after pack()
As you can see, there is still some empty room in the JFrame.

The code to fill the gamePanel:

//Fill the panel with landscape
//One part : 50x50 px
//Panel : 1000x500 px
//Width : 20 prts
//Height : 10 prts
@Override
public void paint(Graphics g){
    super.paint(g);
    for(int i = 0; i < 20; i++){
        for(int j = 0; j < 10; j++){
            //Decide which landscape-img should be used
            switch(levelOne[j][i]){
                case 0:g.drawImage(new ImageIcon("resources\\landscape-img\\air.png")
                                    .getImage(), 50 * i, 50 * j, null);
                    break;
                case 1:g.drawImage(new ImageIcon("resources\\landscape-img\\ground.png")
                                    .getImage(), 50 * i, 50 * j, null);
                    break;              
            }
        }
    }
    g.dispose();
}

I do not see why the frame isn't filled fully. Hope someone can help me!

ikhebgeenaccount
  • 353
  • 6
  • 20
  • Seems like the problem is those JFrame insets. The diff between the preferredsize you set and the size value you're getting back is exactly what the insets are. So maybe you need to create an Insets with 0 values and call JFrame.setInsets? – ControlAltDel Sep 26 '14 at 17:50
  • @ControlAltDel Trying right now, thanks for the suggestion. – ikhebgeenaccount Sep 26 '14 at 17:51
  • I also find that at times where the UI is acting up like this, it can help to start going backwards... Instead of calling setContentPane with your panel, how about adding it to the pre-existing contentPane (with the proper layout) and see how that looks? – ControlAltDel Sep 26 '14 at 17:52
  • @ControlAltDel There doesn't seem to be a `setInsets()`...? – ikhebgeenaccount Sep 26 '14 at 17:52
  • @ControlAltDel I don't really understand your second suggestion. Do you mean I should create one image with the 1000x500 picture and put that in the contentPane, or something els? – ikhebgeenaccount Sep 26 '14 at 17:54
  • you're right, there is no setInsets. you should override getInsets – ControlAltDel Sep 26 '14 at 17:57
  • for the second suggestion, my advice is to try to find a state where the layout at least is working, and then incrementally move from that state to what you want to figure out where the problem creeps in – ControlAltDel Sep 26 '14 at 17:58
  • @ControlAltDel I will start trying your second suggestion, since that seems the best way to fix this problem, thanks for your help though! – ikhebgeenaccount Sep 26 '14 at 18:00
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Sep 27 '14 at 01:13

2 Answers2

4

Few hints to help you to solve your problem.

1) If you need your game panel have a preferred size, then just override getPreferredSize() like this:

public class GamePanel extends JPanel {
    ...
    @Override
    public Dimension getPreferredSize() {
        return isPreferredSizeSet() 
             ? super.getPreferredSize() : new Dimension(1000, 500);
    }
    ...
}

See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

2) Be aware that you should not override paint() method but paintComponent() instead. See A Closer Look at the Paint Mechanism.

public class GamePanel extends JPanel {
    ...
    @Override
    protected void paintComponent(Graphics g) {
       // Your implementation here
    }
    ...
}

3) As already suggested just add your gamePanel to the frame's content pane directly:

frame.getContentPane().add(gamePanel);

This way you will take advantage of its default Layout Manager: BorderLayout to make your panel fill the whole center area.

4) Finally, while you can perfectly use Swing to make games it's not the best API to do so because it is designed for desktop applications. You might want to consider use some library intended to games development such as

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • I don't really understand the syntax at your first point, and Eclipse doesn't seem to understand as well. Could you explain why you use a `void` with `return`? And the question mark? – ikhebgeenaccount Sep 27 '14 at 09:50
  • Found the question mark [here](http://stackoverflow.com/questions/10336899/what-is-a-question-mark-and-colon-operator-within-the-parentheses-of-a-p) – ikhebgeenaccount Sep 27 '14 at 10:00
  • That's exactly what question mark means. About 'void' it's actually a mistake (my bad) and it should be 'Dimension' instead. I'll fix it as soon as I can. @ikhebgeenaccount – dic19 Sep 27 '14 at 13:08
0

Found the answer here. I called frame.setResizable(false) after I called frame.pack(), apparently that messes the frame up. You have to call that before pack() (or `setVisible(true)).

Community
  • 1
  • 1
ikhebgeenaccount
  • 353
  • 6
  • 20