Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
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  
  Why is my JPanel too big?  (Read 1969 times)
0 Members and 1 Guest are viewing this topic.
Offline Ultraviper

Junior Newbie





« Posted 2009-01-29 08:52:52 »

I'm trying to make my first game and I'm having a strange problem. In my panel class I have
1  
2  
   private static final int WIDTH = 640;
   private static final int HEIGHT = 480;


and I use these for both creating the panel

1  
setPreferredSize(new Dimension(WIDTH, HEIGHT));


and creating an image to draw to the screen.

1  
image = createImage(WIDTH,HEIGHT);


 But for some reason when I run the program it makes the panel 650 by 490 which is 10 pixels too big in both directions. I have no idea what is causing this. I have fixed it by changing the panel constructor to

1  
setPreferredSize(new Dimension(WIDTH-10, HEIGHT-10));


but I don't understand why I need to be doing such a thing.

I've tried using different dimensions and no matter what I pick, both sides are always 10 pixels too big, except that the width never goes below 118. When I tried WIDTH = 1 and HEIGHT = 1, I ran my program and it gave me a panel that was 118 by 11.
Offline ctrl-alt-dev

Senior Newbie




Today, my bedroom. Tomorrow the World !


« Reply #1 - Posted 2009-01-29 09:50:24 »

It depends on what layout manager you're using on the component your adding your panel to.
See this: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

The quick fix might actually be to use
setMinimumSize(..)
setMaximumSize(..)
as well. If that does not work you can try to set the layout manager on the container of your panel to null and then use setLocation(..) and setSize(..) to place the component manually. It won't resize with the window through.
See this: [url]http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html[url]
Offline SunshineKiller

Junior Member





« Reply #2 - Posted 2009-01-29 15:34:37 »

for a easy to use layout manager you can try MigLayout google it, its really easy Smiley

<b>Check out my Development Blog:</b> <a href="http://www.scottscreations.com">Scotts Creations</a> | <b>Games in Development: </b> <a href="http://mechwarfare.scottscreations.com">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #3 - Posted 2009-01-29 16:39:14 »

I can't tell from the code you have shown.  If you show your complete code there might be something obvious.

Offline Ultraviper

Junior Newbie





« Reply #4 - Posted 2009-02-02 07:00:56 »

I think I found the problem.
Calling setResizable after calling pack() within the JFrame constructor made the JPanel bigger than it needed to be. Still not sure why or how the frame would add extraneous pixels to my panel dimensions but it's fixed.
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.

xsi3rr4x (16 views)
2014-04-15 18:08:23

BurntPizza (14 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (180 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!