Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine repaints in Swing?

I am calling repaint a bunch of times from a listeners, but the way I designed my paint function is only one repaint is required. I generate a bunch of repaints, since it hooked into my mouse motion listener.

Is there a way to cancel all pending repaints for a certain component? I can't just start ignoring repaints, since some are valid like when you resize the frame or restore it from minimize.

Why do I care? Because my paint code is very heavy and I can't do full repaints at a very high FPS.

like image 216
Pyrolistical Avatar asked Feb 01 '26 12:02

Pyrolistical


2 Answers

Swing will combine repaints for you: see "Painting in AWT and Swing" on Sun's website. If you schedule a number of repaints in rapid succession, they'll get combined into a single call to paintImmediately().

like image 93
Sbodd Avatar answered Feb 04 '26 06:02

Sbodd


Frequent repaint requests are automatically collapsed into one. The best way to optimize this is not to repaint the whole thing, but call repaint with coordinates of specific area. This means you repaint only the area which actually changed.

like image 29
Eugene Ryzhikov Avatar answered Feb 04 '26 05:02

Eugene Ryzhikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!