srecord noeq VolatileImageMakerForComponent(Component component, int w, int h, Renderable renderable) { VolatileImage vImg; long contentsLostCounter; event fireContentsLost; // rendering to the image void renderOffscreen() { do { if (vImg.validate(component.getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE) // old vImg doesn't work with new GraphicsConfig; re-create it vImg = component.createVolatileImage(w, h); Graphics2D g = vImg.createGraphics(); renderable.renderOn(g, w, h); g.dispose(); } while (contentsLost()); } void paintOnScreen(Graphics2D gScreen) { do { if (vImg == null) vImg = component.createVolatileImage(w, h); int returnCode = vImg.validate(component.getGraphicsConfiguration()); if (returnCode == VolatileImage.IMAGE_RESTORED) { // Contents need to be restored renderOffscreen(); } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) { // old vImg doesn't work with new GraphicsConfig; re-create it vImg = component.createVolatileImage(w, h); renderOffscreen(); } gScreen.drawImage(vImg, 0, 0, component); } while (contentsLost()); } bool contentsLost() { if (!vImg.contentsLost()) false; ++contentsLostCounter; fireContentsLost(); true; } bool sizeIs(int w, int h) { ret this.w == w && this.h == h; } }