CachedPainterpublic abstract class CachedPainter extends Object A base class used for icons or images that are expensive to paint.
A subclass will do the following:
- 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.
- Override
paintToImage to render the image. The code that
lives here is equivalent to what previously would go in
paintIcon , for an Icon .
|
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.
getCache(getClass()).setMaxCount(cacheCount);
|
Methods Summary |
---|
protected java.awt.Image | createImage(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.
if (config == null) {
return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}
return config.createCompatibleVolatileImage(w, h);
| private static javax.swing.plaf.metal.CachedPainter$Cache | getCache(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 void | paint(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 void | paintImage(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.
g.drawImage(image, x, y, null);
| protected abstract void | paintToImage(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.
|
|