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:

  1. a high frame rate virtually never required
  2. the frame rate not guaranteed (repaint not directly call painting method, causes call component's paint method as possible' (which may not immediately))
  3. 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

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -