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!
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.
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 ]
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.
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?
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?
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.
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!
Post by:autobot
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, the permaculture playing cards