java - Relationship between paint, paintComponent, repaint, etc -


the paint method:

invoked swing draw components. applications should not invoke paint directly,  should instead use repaint method schedule component redrawing.  method delegates work of painting 3 protected methods:  paintcomponent, paintborder, , paintchildren. they're called in order listed  ensure children appear on top of component itself. speaking, component , children should not paint in insets area allocated border. subclasses can override method, always. subclass  wants specialize ui (look , feel) delegate's paint method should  override paintcomponent.  parameters:     g graphics context in paint  public void paint(graphics g) 

i've read many times not override paint(), override paintcomponent() instead. seen above, documentation suggests override paintcomponent() if want specialize ui.

so, wanted trace through code see why so.

 protected void paintcomponent(graphics g)   {      if (ui != null)       {          graphics scratchgraphics = (g == null) ? null : g.create();           try           {              ui.update(scratchgraphics, this);          }                    {              scratchgraphics.dispose();          }      }  } 

there many methods trace through, conciseness, here's update():

public void update(graphics g, jcomponent c)  {     if (c.isopaque())      {         g.setcolor(c.getbackground());         g.fillrect(0, 0, c.getwidth(),c.getheight());     }      paint(g, c); } 

an overloaded version of paint() called, , paint(), itself, calls paintcomponent(). so, guess i'm trying figure out what's going on here exactly. i'm new swing; there lot of classes , it's pretty difficult trace through of them, limited knowledge of using guis, in general.

are these methods calling each other, thereby giving illusion of image on screen? if so, why matter if override paint() instead of paintcomponent? imagine logic in way flawed or lacking, considering applications, documentation advises against invoking paint() directly, , instead suggests invoke repaint(). so, basically, i'm trying insight overall relationship between paint(), paintcomponent(), repaint(), etc.

firstly take at...

the second should compulsory reading swing developers.

paint (within context of question) top level method called (update called first, forwards paint) when repaintmanager wants component painted.

paint responsible (amongst other things) calling paintcomponent, paintborder , paintchildren. if you're not careful compromise components ability paint. if have override paint, make sure calling super.paint.

it's important note, if child component updated, parent component may not repainted, don't try , use paint provide overlays component.

paintcomponent responsible painting "background" of component. delegates , feel.

in case of jpanel, fill background background color (if set opaque). in case of jtable, paint rows, columns , cell values.

it important call super.paintcomponent, when dealing transparent components.

repaint make request repaintmanager may consolidate paint requests reduce actual number of paint events raised. helps improve performance.

the important thing note here don't control paint process, don't try, work instead.

your paint method expected run quickly, avoid time consuming operations, loading resources, or compound loops possible. if can, create backing buffers , paint instead

ps- paint fun ;)


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -