srecord noeq VolatileImageMakerForComponent(Component component, int w, int h, Renderable renderable) { VolatileImage vImg; long contentsLostCounter; // 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 (vImg.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; true; } }