Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Actual Size of a JFrame?  (Read 1065 times)
0 Members and 1 Guest are viewing this topic.
Offline ToXSiK

Senior Newbie





« Posted 2011-05-24 02:47:25 »

Why when I do:

Quote
gameFrame.setPreferredSize(new Dimension(800, 600));

It gives me a frame about 10 pixels less on the left and the right :/

I have done setSize method as well to no prevail.

How can I fix this so the graphics component embedded in the JFrame is actually 800 x 600

Offline philfrei
« Reply #1 - Posted 2011-05-24 03:08:08 »

Why? Because the size includes the border area. Add a bit for the border edges. I'm not sure how to determine exactly how much. It might change based upon different "look & feels". I usually add 32 to the y-dim (JFrame vs JPanel to display on it).

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline loom_weaver

JGO Coder


Medals: 16



« Reply #2 - Posted 2011-05-24 03:48:17 »

Call setPreferredSize on the component embedded in the JFrame instead of the JFrame itself.

Then call JFrame.pack().
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline ToXSiK

Senior Newbie





« Reply #3 - Posted 2011-05-24 04:20:38 »

Call setPreferredSize on the component embedded in the JFrame instead of the JFrame itself.

Then call JFrame.pack().

I tried that and it didn't work =/

I want it to be exact so if they game were ever to go fullscreen it wouldn't have weird offsets, and I feel like it should fit to the size I specify.  Stupid swing.
Offline Z-Man

Senior Member


Medals: 9



« Reply #4 - Posted 2011-05-24 04:59:22 »

Did you set the preferred size of your embedded component, call pack on the JFrame and then do frame.setVisible(true) ? I seem to remember having some difficulty a while ago with the order of things in Swing. This is how I would do it, replacing the JPanel with whatever your embedded component is.
[CODE]
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
panel.setPrefferedSize(new Dimension(800,600));
frame.add(panel);
frame.pack();
frame.setVisible(true);
[/CODE]
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #5 - Posted 2011-05-24 04:59:27 »

How about running this code after calling setVisible(true):
1  
2  
Insets i = frame.getInsets();
frame.setSize(width+i.right+i.left,height+i.bottom+i.top);

Offline ToXSiK

Senior Newbie





« Reply #6 - Posted 2011-05-24 05:45:55 »

How about running this code after calling setVisible(true):
1  
2  
Insets i = frame.getInsets();
frame.setSize(width+i.right+i.left,height+i.bottom+i.top);


Genius, thanks!  You should add me on AIM or skype, I seem to run into you a lot Cheesy
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #7 - Posted 2011-05-24 05:56:00 »

How about running this code after calling setVisible(true):
1  
2  
Insets i = frame.getInsets();
frame.setSize(width+i.right+i.left,height+i.bottom+i.top);


Genius, thanks!  You should add me on AIM or skype, I seem to run into you a lot Cheesy
Haha, glad to help Cheesy

EDIT: Much cleaner code would be to set the preferredSize on the contentPane of the JFrame:
1  
2  
3  
4  
5  
...
frame.getContentPane().setPreferredSize(new Dimension(width,height));
frame.pack();
frame.setVisible(true);
...

Offline philfrei
« Reply #8 - Posted 2011-05-24 06:30:16 »

Ah, "insets" -- so that is how you get the size inside the border area.
Thanks ra4king.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (67 views)
2013-05-17 21:29:12

alaslipknot (76 views)
2013-05-16 21:24:48

gouessej (109 views)
2013-05-16 00:53:38

gouessej (104 views)
2013-05-16 00:17:58

theagentd (115 views)
2013-05-15 15:01:13

theagentd (104 views)
2013-05-15 15:00:54

StreetDoggy (149 views)
2013-05-14 15:56:26

kutucuk (172 views)
2013-05-12 17:10:36

kutucuk (170 views)
2013-05-12 15:36:09

UnluckyDevil (179 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.073 seconds with 20 queries.