Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the Width of an JPanel in creation?  RSS feed

 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learning the Basics of Java I'm trying the following exercise:

write a program that starts off showing a ball in the middle of the the screen and 2 buttons: left, right
have the ball move to the left or to the right based on the button pressed.
Make sure the ball does not drop of the screen.
Size of the ball and the delta x are pre determined.

I've written 2 classes:
A class that holds main method and creates a JFrame which adds a JPanel of the type MovingBall to its JFrame
A class MovingBall with 3 methods:
- MovingBall(): contains the creation of the Panel with 2 Buttons
- ActionPerformed(): Determines which button was pressed. Calculates new ballxPos calls repaint ()
- paintComponent (Graphics g): paints the actual ball

in the class I declare the following variables:
private JButton Left, Right;
private int ballxPosition = getWidth()/2;
private final int BALLRADIUS = 50;
private final int STEPSIZE = 13;


All these variables are declared at the beginning of the class outside of any methods. The problem I'm having is with the variable ballxPosition. It returns 0 where I would like to get the actual width of the JPanel.
with some Googling I learned that this is supposed to happen because the Panel hasn't been created yet. But all alternatives given as solution feel all kind of janky.

I did think up of an alternative way and initialize the variable within paintComponent and add a variable the tracks the amount off times I have to offset to left or right. however this feels very janky as well.

What is a clean spot to get the Width of the screen before I kick off any recurring methods (ie: actionPerformed, paintComponent)?

Kind regards,


Newbie Stijn

 
Paul Clapham
Sheriff
Posts: 22504
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as you already found out, you can't know the width of a JPanel until it's been displayed. So all of those solutions you found on line were presumably designed to deal with that issue.

Anyway, asking us to provide a better solution than a list of existing solutions which we haven't seen is kind of, well, not very fruitful, don't you agree?

But let met just suggest that the paintComponent() method is not a bad place for this logic. I say that because you're going to have to deal with the case where the user resizes your panel, and the way that you know that has happened is that the paintComponent() method is called.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul for this reply. It is good to get this just before you go to sleep. It gives one something to wander off with ;).

By the way: I do somewhat agree that a list of existing option could have been fruitful. However they all went into the detail of here is the code, problem solved next. Where the used code contained a lot of methods I haven't heard about yet, even remotely. Plus I was not interested in een piece of code as a reply. I rather get a better understanding on the concept of where I can do what with certain information (like where I will be getting a usefull response of the getWidth() of the JPanel).

With you strong and good hint I ended with the following:
- I can only initialize or reset the x Position of the ball within the paintComponent method
- By default I want to initialize/reset the position of  the bal
- Only when I press a button I do not want to reset the position but actually move the ball

So to do this I: will add a new variable to the class: resetBallxPosition with the default value true.
Within the actionPerformed method I set the variable of resetBallxPosition to false and update the position of the ball based on the button pressed. then I call repaint()
Within the paintComponent method I check resetBallxPosition. When true I reset the position to this.getWidth()/2.
At the end of the paintComponent I set the value of resetBallxPosition back to default true.

I feel this quite an ok solution for it, not too janky at all. However I do still wonder if there is a default way to go with this.


 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: I finished the coding and all is working as planned. In case one is interested I can post the code of the MovingBall class. However in real this code is a little bit Dutch language based, meaning in the variables and comments have dutch in it.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stijn Rensen wrote:In case one is interested I can post the code of the MovingBall class.

If you would like someone to review the code, you can post it here.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not really needed I think. I was just interested in the principal of where to place things in a class. Thanks for the offer though.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!