Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sizing a JFrame correctly...  (Read 1917 times)
0 Members and 1 Guest are viewing this topic.
Offline amablue

Senior Newbie





« Posted 2008-07-15 09:07:02 »

I want to set the size of my JFrame to a specific height and width. However, I don't want the border to be included in the sizing calculations. I want to size the window based off the content of the window.

For example, if I want my content panel to be 800x600, calling this doesn't work:

this.setSize(800, 600); // "this" is the frame

It sets the size, but the content area has about 30 pixels cut off from the height and 6 or so from the width.

I also tried

this.getContentPane().setSize(800, 600);

And that didn't work either. That just made the JFrame the minimum possible size. My understanding is that adding this.pack(); should solve that problem, but I must be misunderstanding what pack() does because it doesn't seem to solve the problem at all.

I've also tried swapping out setSize with setPreferredSize, and also throwing in setMinimumSize, but nothing I do seems to work reliably.

Another related problem is that sometimes even when I manually set the size of the JFrame the contentpane doesn't know what size it is. I'll call this.getHeight() or something similar from within the contentpane and it returns 0.

What am I doing wrong here?
Offline Juriy

Junior Devvie





« Reply #1 - Posted 2008-07-15 10:26:54 »

Try this snippet:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class FrameSize extends JFrame {

   public FrameSize() {
      getContentPane().setPreferredSize(new Dimension(100, 200));
      pack();
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
   }
   
   public static void main(String[] args) {
      new FrameSize();
   }
}


Quote
but I must be misunderstanding what pack() does

It tries to lay components in the way that every component is of it's "prefferedSize".

http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline amablue

Senior Newbie





« Reply #2 - Posted 2008-07-15 16:04:54 »

100 by 200 is not wide enough for the title bar, so I bumped it up to 200x200. Using that code my window's content pane is 202 pixels in both directions, and the content pane doesn't know how large it is when it's initialized.

The way my game works is that I each menu screen or gameplay screen is a different JPanel, and I switch the current ContentPane in the JPanel to the appropriate game screen. One of these JPanels needs to know it's own size

I can throw in a hack where I have a public variable in the JFrame that represents the intended size and then have the ContentPane use that in place of getHeight and getWidth, but that doesn't seem like a good solution.  Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #3 - Posted 2008-07-15 16:43:42 »

100 by 200 is not wide enough for the title bar, so I bumped it up to 200x200. Using that code my window's content pane is 202 pixels in both directions, and the content pane doesn't know how large it is when it's initialized.

Are you sure about that?  What OS?  On XP, I get a content pane of exactly 200x200 when I use that method.  I just changed the constructor to:

1  
2  
3  
4  
5  
6  
7  
   public FrameSize() {
      getContentPane().setPreferredSize(new Dimension(200,200));
      pack();
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
System.out.println(getContentPane().getSize());
   }


I even took a screenshot and ensured that the content pane was indeed 200x200...
Offline Wildern

Junior Devvie





« Reply #4 - Posted 2008-07-15 20:44:09 »

I use getInsets() to find out how much of a border I have as it varies from platform to platform.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public class GameApplet extends Applet {
    private static JFrame parentFrame=null;

   public GameApplet() {
      super();
      blnApplet = true;
   }

   public GameApplet(JFrame f) {
      this();
      parentFrame = f;
      blnApplet = false;
   }

   public static void main(String[] args) {
      GameFrame frame = new GameFrame(gameName);
      GameApplet game = new GameApplet(frame);
      frame.add("Center", game);
      game.start();
   }

        public void addNotify() {
      super.addNotify();
      frameWidth = 800;
      frameHeight = 600;
      if (!blnApplet && parentFrame != null) {
              parentFrame.setSize(parentFrame.getInsets().left + parentFrame.getInsets().right + frameWidth
                  ,parentFrame.getInsets().top + parentFrame.getInsets().bottom + frameHeight);
                }
   }
}


public class GameFrame extends JFrame {
   public GameFrame(String str) {
      super (str);
   }
}
Offline DragonsRage

Senior Newbie





« Reply #5 - Posted 2008-07-16 16:04:32 »

I remember having this issue when I first started game programming in Java. The issue is that when when you set the frame size, you are setting the size of the whole frame including the border so this will mess with your calculations. Also, the content pane is not considered when you call pack, only the components added to it are, so setting its size will not help you. With that in mind, here is a solution:

1  
2  
3  
4  
5  
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 600));

frame.add(panel);
frame.pack();


This creates a JPanel which is like the content pane, and adds it to the frame. You can then render to the JPanel and not worry about your frame and its decorations.  Smiley

The pack() method squeezes the window down to the minimum possible size while still allowing all of the components their requested sizes (this is what setPrefferedSize(Dimension dim) does). In other words, pack does not consider the content of your window, but rather the content of the contentPane, so unless something has been added, pack will hide everything but the border and title bar.

Also make sure you call pack AFTER adding your components and setting their preferred sizes, I made that mistake once and it took me forever to figure out what was wrong.  Tongue

If you read https://fivedots.coe.psu.ac.th/~ad/jg/ch1/ch1.pdf , I think you will have a better understanding of how this works, and a decent framework for a game.

Hope this helps.

Secrets and lies

     () ()
     (O.o)
     (>.<)
Offline Juriy

Junior Devvie





« Reply #6 - Posted 2008-07-17 07:40:38 »

1  
In other words, pack does not consider the content of your window, but rather the content of the contentPane, so unless something has been added, pack will hide everything but the border and title bar.


How could that be? The code I've posted before creates no additional components. Only the preferred size of a content pane is set. Try it out: the frame is of normal size.

http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #7 - Posted 2008-07-17 18:33:28 »

Juriy is correct.  Setting the preferred size of the content pane is all you'll need to do.  No need for convoluted calculations involving frame insets, etc.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!