aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JPanel size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel size" Watch "JPanel size" New topic
Author

JPanel size

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
Hi
I don't understand why i don't get my screen size (it gives me 0) with the same way that it get it in my paint method. In paintComponent i have a function fillRect(0, 0, getWidth(), getHeight(), this); nad it works and fills my screen with given colour. But in same class, in constructor i want to give values to int x, y; , so that x=getWidth()/2 and y=getHeight()/2 -- but it gives 0. What's the problem? How should i assign those values to x and y?
code:

[ February 28, 2004: Message edited by: Juhan Voolaid ]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
a) getWidth() & getHeight() will return the size of your gamePanel, not the size of your screen.
b) the first call to getWidth() & getHeight() occurs in your constructor. At this time the height & width is zero. Later on (presumbly after you've added the gamePanel to another container) paintComponent() gets called and by that time your gamePanel's height & width is not zero.
D.
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
yes i ment the size of the panel not the whole screen - thnx, but what should i do to know where the center is after i have created the gamePanel object?
[ February 28, 2004: Message edited by: Juhan Voolaid ]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Just calculate it when you need it :

D.
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK i still have difficulties to solve this problem.
U told to use this method getCenter() by creating new Dimension, but it still gives 0.
I want to use it only when i have created the object - then i suppose in constructor.
Could u please do this code so that. When i have created the gamePanel class - i have 2 int-s x and y that are the center cordinates of this panel.

I'm sure it is one simple thing that i don't understand clearly. Please help.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
don't put the code in the constructor. When the panel is constructed it does not have a width or a height as it's not displayed anywhere.
My advice was to calculate the centre of the screen whenever you need to use it.
Does that make sense ? If not show me the code where you use the centre of the screen and I'll show you what I mean.
D.
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
OK i see now. But what i want to do is - is so that if my program starts - there is a image at the center position. So my class code is similar to what i gave u as a sample. Only it has loaded a image and has paintComponent() method. But in the paintComponent is drawImage() and it takes arguments int x and y as cordinates. So thats why i want to create x, y dynamically.
ok here is code:
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Why can't you just do this ?

D.
[ March 02, 2004: Message edited by: Don Kiddick ]
Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
because i will use drawImage() later when i animate the image by calling repaint(). Thats's why i want x and y to have values.
[ March 02, 2004: Message edited by: Juhan Voolaid ]
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
so, everytime you use drawImage, just do this :

I don't see the problem.
D.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
From your other post, I've realised what you want to do now. Try lazily instantiating the position, like this :

Always use getHeroPosition() rather than accessing the instance variable directly.
does that help ?
D.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel size
 
Similar Threads
Tough Question on Images and Tiles
KeyEvent problem on a JPanel
Help With Repaint Method
Trying to draw image, but not showing up
Checkerboard w/ 64 squares