repaint() problems

  • Thread starter testing1234567891011
  • Start date
T

testing1234567891011

I'm writing a swing application that needs to repaint() different
rectangular areas on a JPanel inside a loop. I've noticed, however,
that when a call to repaint() is in a loop, only the last call to
repaint() (in the last iteration) is actually called. Being that the
other threads responsible for calling paint() or update() on the
component somehow fall through, I tried putting a Thread.sleep(x)
before repainting. With this arrangement, only some of the repaint()s
work, but rarely all of them. What do I do to get repaint() to work
reliably inside of a loop?

Thanks in advance,
Rob
 
J

jan V

work, but rarely all of them. What do I do to get repaint() to work
reliably inside of a loop?

Which JRE/JDK are you on?

Have you looked at whether javax.swing.RepaintManager is any help to you?
 
K

Knute Johnson

I'm writing a swing application that needs to repaint() different
rectangular areas on a JPanel inside a loop. I've noticed, however,
that when a call to repaint() is in a loop, only the last call to
repaint() (in the last iteration) is actually called. Being that the
other threads responsible for calling paint() or update() on the
component somehow fall through, I tried putting a Thread.sleep(x)
before repainting. With this arrangement, only some of the repaint()s
work, but rarely all of them. What do I do to get repaint() to work
reliably inside of a loop?

Thanks in advance,
Rob

Multiple repaints of the same area will be coalesced. How fast are you
trying to call repaint()? If you need to control the painting to get an
exact series of paints, use paintImmediately() on Swing components.
paintImmediately() must be called from the EDT.

See this document on painting:

http://java.sun.com/products/jfc/tsc/articles/painting/index.html
 
R

Roedy Green

I'm writing a swing application that needs to repaint() different
rectangular areas on a JPanel inside a loop. I've noticed, however,
that when a call to repaint() is in a loop, only the last call to
repaint() (in the last iteration) is actually called. Being that the
other threads responsible for calling paint() or update() on the
component somehow fall through, I tried putting a Thread.sleep(x)
before repainting. With this arrangement, only some of the repaint()s
work, but rarely all of them. What do I do to get repaint() to work
reliably inside of a loop?

All repaint does is enqueue a work request packet. If you are tying up
the Swing thread, which is sounds like you are doing with your loop,
Swing can't get around to dealing with the work.

Further, when Swing finally does get some time to service the
requests, it consolidates them to avoid redundant work.

It will all begin to make sense if you put a tiny piece of debug code
in your paint method to display the clip region.
Rectangle r = g.getClipBounds();

see http://mindprod.com/jgloss/repaint.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
T

Thomas Weidenfeller

I'm writing a swing application that needs to repaint() different
rectangular areas on a JPanel inside a loop. I've noticed, however,
that when a call to repaint() is in a loop, only the last call to
repaint() (in the last iteration) is actually called.

Works as designed and documented. Multiple update events are supposed to
be fold into on if possible.

http://java.sun.com/products/jfc/tsc/articles/painting/index.html
work, but rarely all of them. What do I do to get repaint() to work
reliably inside of a loop?

It is reliable. It just doesn't do what you expect it to do. Since you
didn't tell us what you try to achieve with your loop, it is difficult
to suggest a solution.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top