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

repaint() in loop problem  RSS feed

 
Bob Samson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a Swing application that needs to be able to repaint() rectangular regions on a JPanel many times in a loop. Because repaint() only calls the component's paint() or update() method "as soon as possible," the repaint() does not work reliably in a loop. I tried to put a Thread.sleep() before repaint(), but it still does not always work. Sometimes it works perfectly, sometimes only some of the repaints work, and sometimes nothing is ever repainted. Why is this happening and what do I do to be able to repaint() in a loop reliably?

Thank you,
Bob
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bob Samson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay,

I've found that calling paintImmediately(x,y,w,h) instead of repaint(x,y,w,h) in a loop will not collapse into one call to update() or paint().

The code posted above is a little different than what I need to do:

Basically, I need to go through each row in the panel, see if certain conditions are met for each pixel and then update the panel accordingly.

Although my code is working fine now, will repaint() work in a loop of it is given its own thread, as illustrated in the previous post, when the component needs to be updated numerous times in a 1x1 rectangle?

Thanks,
Bob
[ July 22, 2005: Message edited by: Bob Samson ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered using an offscreen image? You can test and make changes to the image in a method and when you're ready call repaint to render the image (in paintComponent). This may help you avoid fighting with swing about the timing of repaints.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!