Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cant seem to draw bouncing balls  RSS feed

 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm trying to make multiple bouncing balls. But I can't seem to get them been drawn on screen. All it does is getting stuck





 
Carey Brown
Saloon Keeper
Posts: 2536
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got two main()'s, I assume your invoking the one in MightyBall which creates a JFrame, but the only place a Ball is created is in PlayScreen.main(), which is not called.

Ball won't compile because it is not a Swing component and is not draw-able, therefore there is no 'repaint()' method.

Ball.run() has no delay (sleep) so even if it did work it would zip by in a blink of an eye.

Don't use abbreviations like clr, use color instead.

BOX_WIDTH should be in camel-case: boxWidth, or better yet, just width.

 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You've got two main()'s, I assume your invoking the one in MightyBall which creates a JFrame, but the only place a Ball is created is in PlayScreen.main(), which is not called.

Ball won't compile because it is not a Swing component and is not draw-able, therefore there is no 'repaint()' method.

Ball.run() has no delay (sleep) so even if it did work it would zip by in a blink of an eye.

Don't use abbreviations like clr, use color instead.

BOX_WIDTH should be in camel-case: boxWidth, or better yet, just width.



Thank you for the tips. I got it to work now if I comment out the iball.run(); If I keep that in it will get stuck, I think im doing something wrong with the Thread. Any tips?
 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you want to paint the ball on a GUI, in which case you would do well not to use sleep(). That is because most GUIs are not thread‑safe (scroll down the link). I suspect what will happen if you get a Runnable to run and paint the ball, is that the repainting will be so fast (0.1μs per cycle) that your motion will be completed long before the screen can be repainted (a matter of milliseconds). So don't even try a separate Thread for your ball. Try a Timer. You might want to set it to run every 20ms or 33ms or similar. You will want some sort of code in the panel you are painting on to show the ball:-Then you can put something into the actionPerformed method for the listener to add to the TimerIn Java8 you can use a λ insteadI shall let you work out the paint and move methods in the Ball class.
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

I presume you want to paint the ball on a GUI, in which case you would do well not to use sleep(). That is because most GUIs are not thread‑safe (scroll down the link). I suspect what will happen if you get a Runnable to run and paint the ball, is that the repainting will be so fast (0.1μs per cycle) that your motion will be completed long before the screen can be repainted (a matter of milliseconds). So don't even try a separate Thread for your ball. Try a Timer. You might want to set it to run every 20ms or 33ms or similar. You will want some sort of code in the panel you are painting on to show the ball:-Then you can put something into the actionPerformed method for the listener to add to the TimerIn Java8 you can use a λ insteadI shall let you work out the paint and move methods in the Ball class.



I fixed it thank you for the help. Can you do 1 more thing for me, is the code like I made it best practice or am I doing stupid things?





 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

I presume you want to paint the ball on a GUI, in which case you would do well not to use sleep(). That is because most GUIs are not thread‑safe (scroll down the link). I suspect what will happen if you get a Runnable to run and paint the ball, is that the repainting will be so fast (0.1μs per cycle) that your motion will be completed long before the screen can be repainted (a matter of milliseconds). So don't even try a separate Thread for your ball. Try a Timer. You might want to set it to run every 20ms or 33ms or similar. You will want some sort of code in the panel you are painting on to show the ball:-Then you can put something into the actionPerformed method for the listener to add to the TimerIn Java8 you can use a λ insteadI shall let you work out the paint and move methods in the Ball class.



I fixed it thank you for the help. Can you do 1 more thing for me, is the code like I made it best practice or am I doing stupid things?





 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion would fit better in our GUIs forum: shall move it.

That is a lot better isn't it

You asked about best practice …
What does the String clr mean? Don't use a String to represent a colour. Particularly when you are not using it elsewhere. There is something not quite right aboutandtogether. I would suggest you remove clr and its associated methods altogether.
I would suggest you don't call the method in the Ball class paintComponent(). Call it simply paint or similar.
Your beveegBal method only allows the ball to move right. See if you can work out how to make it go left if it reaches the right margin of your display.
Your width and height are not final fields and should therefore be not written in upper‑case as if they were constants.
Don't declare more than one variable per line.
I don't like to see addActionListener(this) or its equivalent in the Timer constructor call. Try this insteadYou can shorten that no end in Java8In which case the panel should not implement action listener.

Always call setVisible after everything else (see line 26).
You appear to have the timer start call in the paintComponent method. Don't do that. Put it somewhere else. Maybe in the constructor.
Call your methods setX getY etc, not setx gety.
Always have a space (or new line) after commas, etc.
 
Carey Brown
Saloon Keeper
Posts: 2536
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BOX_WIDTH should be in camel-case: boxWidth, or better yet, just width.
 
Jos van Tuil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think this discussion would fit better in our GUIs forum: shall move it.

That is a lot better isn't it

You asked about best practice …
. . .


Thank you for for the tips, I changed it all. I didnt know about the -> how is it called? that way I can search what it does exactly
 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have removed most of the quote because you do not normally need to quote the whole of a post.

The -> is often read as, “Goes To,” and is officially called “The Arrow Token”. It is only available in Java8. Start reading about it as part of a λ start reading about it in the Java™ Tutorials. You can also see what people say in our Java8 forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!