// size 1x1 - we need this image to get the Graphics obejct
// for measuring.
BufferedImage measureImage = createImage(1, 1);
// Create a PaintContext to use for measuring
PaintContext measureContext = createPaintContext(imageContext,
measureImage,
requestedProperties,
responseProperties);
// Get a measurement for the requested image
Dimension d = painter.getPreferredSize(measureContext);
int width = d.width;
int height = d.height;
// We're done with the measure image and context - free them up
measureImage.flush();
disposePaintContext(measureContext);
// Now that we know how big the image should be, create the image
// that we'll use for painting
BufferedImage paintImage = createImage(width, height);
// Create a PaintContext to use for drawing
PaintContext paintContext = createPaintContext(imageContext,
paintImage,
requestedProperties,
responseProperties);
// Fill in the image with the surrounding color
Graphics g = paintContext.getPaintGraphics();
Color oldColor = g.getColor();
g.setColor(paintContext.getSurroundingColor());
g.fillRect(0, 0, width, height);
g.setColor(oldColor);
// Paint the image
painter.paint(paintContext, g, 0, 0, width, height);