This may be a bit strange but I’m surprised it has not been asked before.
The repaint() method, according to documentation, sets everything in the bounds of the components as “dirty” and then asynchronously calls a repaint. On the other hand, changing bounds seems to be more efficient - setting only what has changed as dirty before the next repaint call.
I want to get in on that efficiency without having to change the bounds. I know I can call something like
setBounds(getBounds()) but it seems gross, and I’m not even sure it would work as the bounds don’t actually change. (I haven’t tried it yet). How can I trigger an asynchronous repaint call on something who only has had some small modifications, and not repaint the whole thing?
There is an overload of repaint:
void Component::repaint ( Rectangle< int > area )
Marks a subsection of this component as needing to be redrawn.
Note that the whole paint() routine will run, but the Graphics context will clip the backend to save the actual paint operations
Thanks, but that’s specifically for marking an area as dirty, isn’t it? It won’t take care of what’s already dirty?
And I considered it, but for a diagonal line, for example, it would be difficult to mark.
I may just try setBounds to the same bounds, see if it does the trick.
If another repaint() is pending it will coalesce those paint calls. IMHO it is exactly what should happen.
If I remember right, setBounds will not do anything, if the old bounds are the same like the new bounds.
Thanks, I need to take a better look at what’s going on with the paint calls. I thought changing bounds and calling repaint were in practice different, because when I had
repaint() calls where I thought I needed them, the app would run crazy slow.
But changing bounds seems to repaint much more efficiently. Is there a different mechanism? I can’t see one in the code above.