Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  JFrame Size  (Read 1653 times)
0 Members and 1 Guest are viewing this topic.
Offline rolledback

Junior Member


Projects: 1



« Posted 2012-02-29 20:47:27 »

I set the size of my JFrame to 1000x500 but it is obvious to me through the use of my drawing methods that the actual size is not 1000x500 but something slightly smaller. Why is this?

1  
2  
3  
4  
5  
JFrame frame = new JFrame("Jumper");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(1000, 500);
frame.add(new Game());
frame.setVisible(true);
Offline UprightPath
« Reply #1 - Posted 2012-02-29 20:53:53 »

It's because the JFrame has what is called 'Decorations'. These are the portions of the window that are given over to the L&F or the System to paint the bars on top and on the bottom. These will differ depending on the L&F and the system that the game is being run on, so you should not assume that figuring out manually will result in the same thing every time!

Better would be to...

1  
2  
3  
4  
5  
6  
JFrame frame = new JFrame("Jumper");
JComponent component  = new JComponent();
frame.add(component);
component.setSize(1000,500);
component.add(new Game());
frame.setVisible(true);

Offline Cero
« Reply #2 - Posted 2012-02-29 21:05:54 »

Frame.getInsets().top, left, bottom and right gives you the pixel values of the borders

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-03-01 05:10:01 »

Use Insets as Cero mentioned:
1  
2  
3  
4  
5  
6  
7  
JFrame frame = new JFrame("Jumper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Game());
frame.setVisible(true);

Insets i = frame.getInsets(); //you have to get the Insets after you set the frame visible.
frame.setSize(1000 + i.left + i.right, 500 + i.top + i.bottom);

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-03-01 08:36:50 »

Sadly, the examples above are wrong.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
JComponent component  = new JComponent();
component.setPreferredSize(new Dimension(1000,500));
component.add(new Game());

JFrame frame = new JFrame("Jumper");
frame.setLayout(new BorderLayout());
frame.add(component, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // center on screen
frame.setVisible(true);


This will also resize the component when the frame is resized.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #5 - Posted 2012-03-01 14:52:25 »

And all wrapped up in a SwingUtilities.invokeLater().
Offline rolledback

Junior Member


Projects: 1



« Reply #6 - Posted 2012-03-02 01:21:56 »

Wow, lots of replies! I'm not sure who's solution I like the best.  Roll Eyes
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-03-02 02:42:16 »

1  
2  
frame.setLayout(new BorderLayout());
frame.add(component, BorderLayout.CENTER);

Those two lines are unnecessary since a JFrame's default layout is BorderLayout. All you need is frame.add(component);

Also, don't draw on a JPanel! I'm trying to eradicate this practice T____T

Offline rolledback

Junior Member


Projects: 1



« Reply #8 - Posted 2012-03-02 03:56:51 »

Also, don't draw on a JPanel! I'm trying to eradicate this practice T____T

What should I draw on then?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-03-02 03:58:35 »

Using Swing: JComponent.
Pure and raw game: Canvas.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #10 - Posted 2012-03-02 03:58:51 »

JComponent!

Like was stated earlier.

JFrame should only be used as a frame, a container for your other stuff. Painting should always be done on something else that's just for painting. Like a dedicated JComponent. :3

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #11 - Posted 2012-03-02 04:05:21 »

1  
2  
frame.setLayout(new BorderLayout());
frame.add(component, BorderLayout.CENTER);

Those two lines are unnecessary since a JFrame's default layout is BorderLayout. All you need is frame.add(component);

Fair enough, but I think his point was that there's no need to manually account for the insets of the frame, rather you should just set the size of your *content* pane, since that's what you'll render to.

<OT>
When not doing games I usually used frame.setContentPane(myPanel) since myPanel was always some Container subclass.  Saves on one nested component!!!!!!!!!!!!1
</OT>
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.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!