Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JPanel size

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just calculate it when you need it :

D.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't you just do this ?

D.
[ March 02, 2004: Message edited by: Don Kiddick ]
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, everytime you use drawImage, just do this :

I don't see the problem.
D.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
bacon. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic