FileDocCategorySizeDatePackage
CachedPainter.javaAPI DocJava SE 5 API10048Fri Aug 26 14:58:06 BST 2005javax.swing.plaf.metal

CachedPainter

public abstract class CachedPainter extends Object
A base class used for icons or images that are expensive to paint. A subclass will do the following:
  1. Invoke paint when you want to paint the image, if you are implementing Icon you'll invoke this from paintIcon. The args argument is useful when additional state is needed.
  2. Override paintToImage to render the image. The code that lives here is equivalent to what previously would go in paintIcon, for an Icon.
version
@(#)CachedPainter.java 1.2 04/02/15

Fields Summary
private static final Map
cacheMap
Constructors Summary
public CachedPainter(int cacheCount)
Creates an instance of CachedPainter that will cache up to cacheCount images of this class.

param
cacheCount Max number of images to cache

        getCache(getClass()).setMaxCount(cacheCount);
    
Methods Summary
protected java.awt.ImagecreateImage(java.awt.Component c, int w, int h, java.awt.GraphicsConfiguration config)
Creates the image to cache. This returns an opaque image, subclasses that require translucency or transparency will need to override this method.

param
c Component painting to
param
w Width of image to create
param
h Height to image to create
param
config GraphicsConfiguration that will be rendered to, this may be null.

        if (config == null) {
            return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        }
        return config.createCompatibleVolatileImage(w, h);
    
private static javax.swing.plaf.metal.CachedPainter$CachegetCache(java.lang.Object key)



         
        synchronized(cacheMap) {
            Cache cache = cacheMap.get(key);
            if (cache == null) {
                cache = new Cache(1);
                cacheMap.put(key, cache);
            }
            return cache;
        }
    
protected voidpaint(java.awt.Component c, java.awt.Graphics g, int x, int y, int w, int h, java.lang.Object args)
Renders the cached image to the the passed in Graphic. If there is no cached image paintToImage will be invoked. paintImage is invoked to paint the cached image.

        if (w <= 0 || h <= 0) {
            return;
        }
        Object key = getClass();
        GraphicsConfiguration config = c.getGraphicsConfiguration();
        Cache cache = getCache(key);
        Image image = cache.getImage(key, config, w, h, args);
        int attempts = 0;
        do {
            boolean draw = false;
            if (image instanceof VolatileImage) {
                // See if we need to recreate the image
                switch (((VolatileImage)image).validate(config)) {
                case VolatileImage.IMAGE_INCOMPATIBLE:
                    ((VolatileImage)image).flush();
                    image = null;
                    break;
                case VolatileImage.IMAGE_RESTORED:
                    draw = true;
                    break;
                }
            }
            if (image == null) {
                // Recreate the image
                image = createImage(c, w, h, config);
                cache.setImage(key, config, w, h, args, image);
                draw = true;
            }
            if (draw) {
                // Render to the Image
                Graphics g2 = image.getGraphics();
                paintToImage(c, g2, w, h, args);
                g2.dispose();
            }

            // Render to the passed in Graphics
            paintImage(c, g, x, y, w, h, image, args);

            // If we did this 3 times and the contents are still lost
            // assume we're painting to a VolatileImage that is bogus and
            // give up.  Presumably we'll be called again to paint.
        } while ((image instanceof VolatileImage) &&
                 ((VolatileImage)image).contentsLost() && ++attempts < 3);
    
protected voidpaintImage(java.awt.Component c, java.awt.Graphics g, int x, int y, int w, int h, java.awt.Image image, java.lang.Object[] args)
Paints the image to the specified location.

param
c Component painting to
param
g Graphics to paint to
param
x X coordinate to paint to
param
y Y coordinate to paint to
param
w Width to paint to
param
h Height to paint to
param
image Image to paint
param
args Arguments supplied to paint

        g.drawImage(image, x, y, null);
    
protected abstract voidpaintToImage(java.awt.Component c, java.awt.Graphics g, int w, int h, java.lang.Object[] args)
Paints the representation to cache to the supplied Graphics.

param
c Component painting to
param
g Graphics to paint to
param
w Width to paint to
param
h Height to paint to
param
args Arguments supplied to paint