java - Is calling repaint from paintComponent a good practice -
for ui components in our application, override paintcomponent
, under conditions "recursively" calls invoking repaint
. use approach achieve high refresh rate of animations in component.
for instance, progress bar use looks like:
public class simpleprogressbar extends jpanel { private boolean inprogress; .... @override protected void paintcomponent(graphics g) { if (inprogress) { paintbar(g); repaint(); } else { dosomeotherthings(); } } }
is practice (especially in terms of performance / efficiency / cpu usage)?
better use timer
or background thread repaint
our components?
is practice (especially in terms of performance / efficiency / cpu usage)?
no, not practice. calling repaint within paintcomponent
bad practice because:
- a high frame rate virtually never required
- the frame rate not guaranteed (repaint not directly call painting method, causes call component's paint method as possible' (which may not immediately))
- places priority on painting of single component, , can result in poor performance not in painting of 1 component, painting of other components response other edt specific tasks (eg events)
is better use timer or background thread repaint our components?
yes, using timer
or thread
gives better control on frame rate, without bogging down edt while doing so. depending upon context, timer
runs on edt (as opposed thread) no dispatching edt required.
Comments
Post a Comment