Tuesday, 24 April 2012

Painting in AWT and Swing

AWT stands for abstract windowing toolkit. When the original AWT API was developed for JDK 1.0, only heavyweight components existed ("heavyweight" means that the component has its own opaque native window). Lightweight components are introduced in JDK 1.1 (a "lightweight" component is one that reuses the native window of its closest heavyweight ancestor). There are subtle differences in how painting works for heavyweight and lightweight components.

Swing Painting Guidelines

  1. For Swing components, paint() is always invoked as a result of both system-triggered and app-triggered paint requests; update() is never invoked on Swing components.
  2. Programs may trigger a future call to paint() by invoking repaint(), but shouldn't call paint() directly.
  3. On components with complex output, repaint() should be invoked with arguments which define only the rectangle that needs updating, rather than the no-arg version, which causes the entire component to be repainted.
  4. Swing's implementation of paint() factors the call into 3 separate callbacks:
    1. paintComponent()
    2. paintBorder()
    3. paintChildren()
    Extensions of Swing components which wish to implement their own paint code should place this code within the scope of the paintComponent() method (not within paint()).
Reference:

No comments :

Post a Comment