Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update a JPanel with runnables there are created at runtime

 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:




 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic