swingx
Version 2005-08-19

org.jdesktop.swingx.painter
Class AbstractPainter<T extends JComponent>

java.lang.Object
  extended by org.jdesktop.swingx.JavaBean
      extended by org.jdesktop.swingx.painter.AbstractPainter<T>
All Implemented Interfaces:
Painter<T>
Direct Known Subclasses:
AbstractGradientPainter, BackgroundPainter, CheckerboardPainter, CompoundPainter, GlossPainter, ImagePainter, MattePainter, PinstripePainter, ShapePainter, TextPainter

public abstract class AbstractPainter<T extends JComponent>
extends JavaBean
implements Painter<T>

A convenient base class from which concrete Painter implementations may extend. It extends JavaBean and thus provides property change notification (which is crucial for the Painter implementations to be available in a GUI builder). It also saves off the Graphics2D state in its "saveState" method, and restores that state in the "restoreState" method. Sublasses simply need to extend AbstractPainter and implement the paintBackground method.

For example, here is the paintBackground method of BackgroundPainter:


  public void paintBackground(Graphics2D g, JComponent component) {
      g.setColor(component.getBackground());
      g.fillRect(0, 0, component.getWidth(), component.getHeight());
  }
 

AbstractPainter provides a very useful default implementation of the paint method. It:

  1. Saves off the old state
  2. Sets any specified rendering hints
  3. Sets the Clip if there is one
  4. Sets the Composite if there is one
  5. Delegates to paintBackground
  6. Restores the original Graphics2D state
    1. Specifying rendering hints can greatly improve the visual impact of your applications. For example, by default Swing doesn't do much in the way of antialiasing (except for Fonts, but that's another story). Pinstripes don't look so good without antialiasing. So if I were going to paint pinstripes, I might do it like this:

      
         PinstripePainter p = new PinstripePainter();
         p.setAntialiasing(RenderingHints.VALUE_ANTIALIAS_ON);
       

      You can read more about antialiasing and other rendering hints in the java.awt.RenderingHints documentation. By nature, changing the rendering hints may have an impact on performance. Certain hints require more computation, others require less


      Constructor Summary
      AbstractPainter()
                Creates a new instance of AbstractPainter
       
      Method Summary
       Object getAlphaInterpolation()
                 
       Object getAntialiasing()
                 
       Shape getClip()
                 
       Object getColorRendering()
                 
       Composite getComposite()
                 
       Object getDithering()
                 
       Effect[] getEffects()
                 
       Object getFractionalMetrics()
                 
       Object getInterpolation()
                 
       Object getRendering()
                 
       Object getRenderingHint(RenderingHints.Key key)
                 
       Map<RenderingHints.Key,Object> getRenderingHints()
                 
       Resize getResizeClip()
                 
       Object getStrokeControl()
                 
       Object getTextAntialiasing()
                 
       boolean isUseCache()
                 
       void paint(Graphics2D g, T component)
                Paints on the given Graphics2D object some effect which may or may not be related to the given component.
      protected abstract  void paintBackground(Graphics2D g, T component)
                Subclasses should implement this method and perform custom painting operations here.
      protected  void restoreState(Graphics2D g)
                Restores previously saved state.
      protected  void saveState(Graphics2D g)
                Saves the state in the given Graphics2D object so that it may be restored later.
       void setAlphaInterpolation(Object alphaInterpolation)
                Sets the technique used for interpolating alpha values.
       void setAntialiasing(Object antialiasing)
                Sets whether or not to antialias
       void setClip(Shape clip)
                Specifies the Shape to use for clipping the painting area.
       void setColorRendering(Object colorRendering)
                Sets the technique to use for rendering colors
       void setComposite(Composite c)
                Sets the Composite to use.
       void setDithering(Object dithering)
                Sets whether or not to dither
       void setEffects(BufferedImageOp... filters)
                A convenience method for specifying the effects to use based on BufferedImageOps.
       void setEffects(Effect... effects)
                Sets the effects to apply to the results of the AbstractPainter's painting operation.
       void setFractionalMetrics(Object fractionalMetrics)
                Sets whether or not to use fractional metrics
       void setInterpolation(Object interpolation)
                Sets the technique to use for interpolation (used esp. when scaling)
       void setRendering(Object rendering)
                Specifies a hint as to techniques to use with regards to rendering quality vs. speed
       void setRenderingHint(RenderingHints.Key key, Object hint)
                Set the given hint for the given key.
       void setRenderingHints(Map<RenderingHints.Key,Object> renderingHints)
                Sets the rendering hints to use.
       void setResizeClip(Resize r)
                Specifies the resize behavior of the clip.
       void setStrokeControl(Object strokeControl)
                Specifies a technique for rendering strokes
       void setTextAntialiasing(Object textAntialiasing)
                Sets the technique for anti-aliasing text.
       void setUseCache(boolean b)
                Sets whether to cache the painted image with a SoftReference in a BufferedImage between calls.
       
      Methods inherited from class org.jdesktop.swingx.JavaBean
      addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, getPropertyChangeListeners, hasListeners, removePropertyChangeListener, removePropertyChangeListener
       
      Methods inherited from class java.lang.Object
      clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
       

      Constructor Detail

      AbstractPainter

      public AbstractPainter()
      Creates a new instance of AbstractPainter

      Method Detail

      setUseCache

      public void setUseCache(boolean b)

      Sets whether to cache the painted image with a SoftReference in a BufferedImage between calls. If true, and if the size of the component hasn't changed, then the cached image will be used rather than causing a painting operation.

      This should be considered a hint, rather than absolute. Several factors may force repainting, including low memory, different component sizes, or possibly new rendering hint settings, etc.

      Parameters:
      b - whether or not to use the cache

      isUseCache

      public boolean isUseCache()
      Returns:
      whether or not the cache should be used

      setEffects

      public void setEffects(Effect... effects)

      Sets the effects to apply to the results of the AbstractPainter's painting operation. Some common effects include blurs, shadows, embossing, and so forth. If the given effects is a null array, no effects will be used

      Parameters:
      effects - the Effects to apply to the results of the AbstractPainter's painting operation

      setEffects

      public void setEffects(BufferedImageOp... filters)

      A convenience method for specifying the effects to use based on BufferedImageOps. These will each be individually wrapped by an ImageEffect and then setEffects(Effect... effects) will be called with the resulting array

      Parameters:
      filters - the BufferedImageOps to wrap as effects

      getEffects

      public Effect[] getEffects()
      Returns:
      effects a defensive copy of the Effects to apply to the results of the AbstractPainter's painting operation. Will never null

      setClip

      public void setClip(Shape clip)
      Specifies the Shape to use for clipping the painting area. This may be null

      Parameters:
      clip - the Shape to use to clip the area. Whatever is inside this shape will be kept, everything else "clipped". May be null. If null, the clipping is not set on the graphics object

      getClip

      public Shape getClip()
      Returns:
      the clipping shape

      setResizeClip

      public void setResizeClip(Resize r)
      Specifies the resize behavior of the clip. As with all other properties that rely on Resize, the value of the width/height of the shape will represent a percentage of the width/height of the component, as a value between 0 and 1

      Parameters:
      r - value indication whether/how to resize the clip. If null, Resize.NONE will be used

      getResizeClip

      public Resize getResizeClip()
      Returns:
      value indication whether/how to resize the clip. Will never be null

      setComposite

      public void setComposite(Composite c)
      Sets the Composite to use. For example, you may specify a specific AlphaComposite so that when this Painter paints, any content in the drawing area is handled properly

      Parameters:
      c - The composite to use. If null, then no composite will be specified on the graphics object

      getComposite

      public Composite getComposite()
      Returns:
      the composite

      getAlphaInterpolation

      public Object getAlphaInterpolation()
      Returns:
      the technique used for interpolating alpha values. May be one of:
      • RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED
      • RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY
      • RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT

      setAlphaInterpolation

      public void setAlphaInterpolation(Object alphaInterpolation)
      Sets the technique used for interpolating alpha values.

      Parameters:
      alphaInterpolation - May be one of:
      • RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED
      • RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY
      • RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT

      getAntialiasing

      public Object getAntialiasing()
      Returns:
      whether or not to antialias May be one of:
      • RenderingHints.VALUE_ANTIALIAS_DEFAULT
      • RenderingHints.VALUE_ANTIALIAS_OFF
      • RenderingHints.VALUE_ANTIALIAS_ON

      setAntialiasing

      public void setAntialiasing(Object antialiasing)
      Sets whether or not to antialias

      Parameters:
      antialiasing - May be one of:
      • RenderingHints.VALUE_ANTIALIAS_DEFAULT
      • RenderingHints.VALUE_ANTIALIAS_OFF
      • RenderingHints.VALUE_ANTIALIAS_ON

      getColorRendering

      public Object getColorRendering()
      Returns:
      the technique to use for rendering colors May be one of:
      • RenderingHints.VALUE_COLOR_RENDER_DEFAULT
      • RenderingHints.VALUE_RENDER_QUALITY
      • RenderingHints.VALUE_RENDER_SPEED

      setColorRendering

      public void setColorRendering(Object colorRendering)
      Sets the technique to use for rendering colors

      Parameters:
      colorRendering - May be one of:
      • RenderingHints.VALUE_COLOR_RENDER_DEFAULT
      • RenderingHints.VALUE_RENDER_QUALITY
      • RenderingHints.VALUE_RENDER_SPEED

      getDithering

      public Object getDithering()
      Returns:
      whether or not to dither May be one of:
      • RenderingHints.VALUE_DITHER_DEFAULT
      • RenderingHints.VALUE_DITHER_ENABLE
      • RenderingHints.VALUE_DITHER_DISABLE

      setDithering

      public void setDithering(Object dithering)
      Sets whether or not to dither

      Parameters:
      dithering - May be one of:
      • RenderingHints.VALUE_DITHER_DEFAULT
      • RenderingHints.VALUE_DITHER_ENABLE
      • RenderingHints.VALUE_DITHER_DISABLE

      getFractionalMetrics

      public Object getFractionalMetrics()
      Returns:
      whether or not to use fractional metrics May be one of:
      • RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT
      • RenderingHints.VALUE_FRACTIONALMETRICS_OFF
      • RenderingHints.VALUE_FRACTIONALMETRICS_ON

      setFractionalMetrics

      public void setFractionalMetrics(Object fractionalMetrics)
      Sets whether or not to use fractional metrics

      Parameters:
      fractionalMetrics - May be one of:
      • RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT
      • RenderingHints.VALUE_FRACTIONALMETRICS_OFF
      • RenderingHints.VALUE_FRACTIONALMETRICS_ON

      getInterpolation

      public Object getInterpolation()
      Returns:
      the technique to use for interpolation (used esp. when scaling) May be one of:
      • RenderingHints.VALUE_INTERPOLATION_BICUBIC
      • RenderingHints.VALUE_INTERPOLATION_BILINEAR
      • RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR

      setInterpolation

      public void setInterpolation(Object interpolation)
      Sets the technique to use for interpolation (used esp. when scaling)

      Parameters:
      interpolation - May be one of:
      • RenderingHints.VALUE_INTERPOLATION_BICUBIC
      • RenderingHints.VALUE_INTERPOLATION_BILINEAR
      • RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR

      getRendering

      public Object getRendering()
      Returns:
      a hint as to techniques to use with regards to rendering quality vs. speed May be one of:
      • RenderingHints.VALUE_RENDER_QUALITY
      • RenderingHints.VALUE_RENDER_SPEED
      • RenderingHints.VALUE_RENDER_DEFAULT

      setRendering

      public void setRendering(Object rendering)
      Specifies a hint as to techniques to use with regards to rendering quality vs. speed

      Parameters:
      rendering - May be one of:
      • RenderingHints.VALUE_RENDER_QUALITY
      • RenderingHints.VALUE_RENDER_SPEED
      • RenderingHints.VALUE_RENDER_DEFAULT

      getStrokeControl

      public Object getStrokeControl()
      Returns:
      technique for rendering strokes May be one of:
      • RenderingHints.VALUE_STROKE_DEFAULT
      • RenderingHints.VALUE_STROKE_NORMALIZE
      • RenderingHints.VALUE_STROKE_PURE

      setStrokeControl

      public void setStrokeControl(Object strokeControl)
      Specifies a technique for rendering strokes

      Parameters:
      strokeControl - May be one of:
      • RenderingHints.VALUE_STROKE_DEFAULT
      • RenderingHints.VALUE_STROKE_NORMALIZE
      • RenderingHints.VALUE_STROKE_PURE

      getTextAntialiasing

      public Object getTextAntialiasing()
      Returns:
      technique for anti-aliasing text. (TODO this needs to be updated for Mustang. You may use the new Mustang values, and everything will work, but support in the GUI builder and documentation need to be added once we branch for Mustang)
      May be one of:
      • RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT
      • RenderingHints.VALUE_TEXT_ANTIALIAS_OFF
      • RenderingHints.VALUE_TEXT_ANTIALIAS_ON

      setTextAntialiasing

      public void setTextAntialiasing(Object textAntialiasing)
      Sets the technique for anti-aliasing text. (TODO this needs to be updated for Mustang. You may use the new Mustang values, and everything will work, but support in the GUI builder and documentation need to be added once we branch for Mustang)

      Parameters:
      textAntialiasing - May be one of:
      • RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT
      • RenderingHints.VALUE_TEXT_ANTIALIAS_OFF
      • RenderingHints.VALUE_TEXT_ANTIALIAS_ON

      getRenderingHint

      public Object getRenderingHint(RenderingHints.Key key)
      Returns:
      the rendering hint associated with the given key. May return null

      setRenderingHint

      public void setRenderingHint(RenderingHints.Key key,
                                   Object hint)
      Set the given hint for the given key. This will end up firing the appropriate property change event if the key is recognized. For example, if the key is RenderingHints.KEY_ANTIALIASING, then the setAntialiasing method will be called firing an "antialiasing" property change event if necessary. If the key is not recognized, no event will be fired but the key will be saved. The key must not be null

      Parameters:
      key - cannot be null
      hint - must be a hint compatible with the given key

      getRenderingHints

      public Map<RenderingHints.Key,Object> getRenderingHints()
      Returns:
      a copy of the map of rendering hints held by this class. This returned value will never be null

      setRenderingHints

      public void setRenderingHints(Map<RenderingHints.Key,Object> renderingHints)
      Sets the rendering hints to use. This will replace the rendering hints entirely, clearing any hints that were previously set.

      Parameters:
      renderingHints - map of hints. May be null. I null, a new Map of rendering hints will be created

      saveState

      protected void saveState(Graphics2D g)
      Saves the state in the given Graphics2D object so that it may be restored later.

      Parameters:
      g - the Graphics2D object who's state will be saved

      restoreState

      protected void restoreState(Graphics2D g)
      Restores previously saved state. A call to saveState must have occured prior to calling restoreState, or an IllegalStateException will be thrown.

      Parameters:
      g - the Graphics2D object to restore previously saved state to

      paint

      public void paint(Graphics2D g,
                        T component)
      Description copied from interface: Painter

      Paints on the given Graphics2D object some effect which may or may not be related to the given component. For example, BackgroundPainter will use the background property of the component and the width/height of the component to perform a fill rect. Most other Painters will disregard the component entirely, except to get the component width/height.

      The Graphics2D object must be returned to the same state it started at by the end of the method. For example, if "setColor(c)" was called on the graphics object, it should be reset to the original color before the method returns.

      Specified by:
      paint in interface Painter<T extends JComponent>
      Parameters:
      g - The Graphics2D object in which to paint
      component - The JComponent that the Painter is delegate for.

      paintBackground

      protected abstract void paintBackground(Graphics2D g,
                                              T component)
      Subclasses should implement this method and perform custom painting operations here. Common behavior, such as setting the clip and composite, saving and restoring state, is performed in the "paint" method automatically, and then delegated here.

      Parameters:
      g - The Graphics2D object in which to paint
      component - The JComponent that the Painter is delegate for.

      swingx
      Version 2005-08-19