Forums Register Login

Update a JPanel with runnables there are created at runtime

+Pie Number of slices to send: Send
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:




+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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!
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1941 times.
Similar Threads
How do I save a variable value -Unsolved-
Circle collision Detection: Solved =)
Moving multiple ojects in Java2D
Fluid Motion Moving Circles Smooth -solved-
How to move a second object?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:17:30.