calling repaint from a thread instead of using ...

I have a system for updated my graphics and wondered if theres a smarter way to do it.

Right now I have a custom thread which calls -

SwingUtilities.invokeLater(myGraphicsThing);

myGraphicsThing has a run method which simply calls paintComponent.

The objects I have are sprites and such. So I pass in a list of things to update to my custom thread and it handles things being animated.

I started thinking that instead of implementing Runnable on all these graphic objects, I could simply call the repaint() method on each one. Then if my graphic thread is running faster than the painting, I would get the benefit of multiple calls being collapsed into one by the paintManager.

Is this on track thinking?

Sounds on track to me. Calling repaint() with no parameters will effectively schedule it til later, i.e. repaint as soon as you can. Calling repaint(0) will attempt to repaint within 0 ms, and hence attempt to repaint now.

Kev

passing a runnable into invokeLater, that simply calls paintComponents(g) is functionally identical to calling repaint().

But, yes, repaint events can be collapsed (coalesced) into a single call - so repaint would be a better way. (And its what its there for…)

Ofcourse, whether you should be using passive or active rendering is another issue entirely.

:edit:

posted @ the same time as Kev.

I keep hearing mention of passive versus active rendering. My understanding is that active rendering is where I need/want everything thats need to be drawn done now and I control when the now is.

Passive is when I schedule it or let someone/something else take care of the when, ie the event dispatch thread.

The app I’m doing is for children. Its a learning program with different areas to it. One of the areas has shapes you match. When you match all the shapes, the background spins around behind each shape.

Another area has a boat moving around, which shoots fireworks.

I’m wondering if I should go ahead and pull out my redundant code and let swing handle it, since the ‘thinking’ is already there anyway.

I develop on my 1.3 GigHz laptop, then go test it on my daughters 200Mhz machine. Talk about a stress test! Her poor little box can’t handle refreshing the entire screen as fast as I need it to.

If anyone’s interested in a preview or to test it out on their kids (age 2’ish - 4’ish), let me know. I have no webstart (no web page, no jnlp, = no webstart.) :frowning: But I do plan on getting one setup.

Cheers!