wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Update a JPanel with runnables there are created at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Update a JPanel with runnables there are created at runtime" Watch "Update a JPanel with runnables there are created at runtime" New topic
Author

Update a JPanel with runnables there are created at runtime

Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 57
Hi!

i'm starting to study threads, and i'm stuck in this exercise, i know that swing is not thread-safe and i think that there are more than one thread manipulating the GUI at the same time, i've done some research but i just can fix it, i would apreciate if u guys could give me some tips of how to correct my code

the problem is that sometimes the balls are painted off the bounds of the panel, when there are more than 1 ball (thread), manipulating the panel.

i hope u can understand my doubt, any tips or advices are welcome ... thanks in advance!

here are the classes:






Studying ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Everything looks fine to me... except...

I would recommend that you move the code that changes the direction of the ball, from the painting method to the swing utilities runnable.

The reasoning is, if the events are handled out of order, or redraw requests get consolidated, you can increment/decrement more than once before drawing. If by chance you leave the area, by more than one bit, your code will constantly change direction off of the display area.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 57
works perfectly ... thank you very much henry!!

but considering what you said, that the events may be handled out of order, and now i want the balls to change their directions when they hit each other ... then to implement a colision i have to know where the balls are, right? so how could i implement that if i cant use the paint method to change the balls directions? i thought of an object used as a buffer, and this buffer saves de "position" (x, y) of the balls then my threads would know if they hit each other, i will try to implement that, and i will post the results, but what you guys think? its a good idea?
[ July 29, 2006: Message edited by: Mateus Lucio ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

If you want to have the paint method change directions instead, fine... Other things you can try. Note: they both don't have to be done. These are independent suggestions.

- Don't have changing directions be a toggle. You can have the paint method "set" the direction to the correct direction based on which side it it on. (e.g. at the bottom of the display array, it will be set towards up)

- Don't blindly move the item in the runnable. Check if the item is *not* at the border before you move it.

Henry
Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 57
but i cant use my paint method to change the direction of the ball, if i the toggle code in the paint method, then sometimes the balls are painted out of the bounds of the panel, thats why i thought that using a different implementation it might work, but even if it works, how can i use the paint method to change the directions correctly?
Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 57
but i cant use my paint method to change the direction of the ball, if i the toggle code in the paint method, then sometimes the balls are painted out of the bounds of the panel, thats why i thought that using a different implementation it might work, but even if it works, how can i use the paint method to change the directions correctly?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Originally posted by Mateus Lucio:
but i cant use my paint method to change the direction of the ball, if i the toggle code in the paint method, then sometimes the balls are painted out of the bounds of the panel, thats why i thought that using a different implementation it might work, but even if it works, how can i use the paint method to change the directions correctly?


That is why I suggested that you either (1) don't use toggle (set the direction based on location) or (2) check before you increment / decrement so that you don't leave the display.

Henry
Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 57
Great!!

it really took me a while to understand that, but i got it, i took the second sugestion, checking the steps before increment or decrement the runnables ...

thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Update a JPanel with runnables there are created at runtime
 
Similar Threads
How do I save a variable value -Unsolved-
How to move a second object?
Moving multiple ojects in Java2D
Fluid Motion Moving Circles Smooth -solved-
Circle collision Detection: Solved =)