/** * Renders to a volatile image, and then paints that to the screen. * This helps with scaling performance on accelerated surfaces (e.g. OpenGL) * * @param g the graphics to paint the image to * @param x the left coordinate to start painting at. * @param y the top coordinate to start painting at. * @param w the width of the paint area * @param h the height of the paint area */ private void volatileRender(Graphics g, int x, int y, int w, int h) { do { if (updatePending || volatileImage == null || volatileImage.validate(getGraphicsConfiguration()) != VolatileImage.IMAGE_OK) { bufferLock.lock(); try { updatePending = false; renderVolatileImage(currentImage); } finally { bufferLock.unlock(); } } g.drawImage(volatileImage, x, y, w, h, null); } while (volatileImage.contentsLost()); }
private void renderVolatileImage(BufferedImage bufferedImage) { do { int w = bufferedImage.getWidth(), h = bufferedImage.getHeight(); GraphicsConfiguration gc = getGraphicsConfiguration(); if (volatileImage == null || volatileImage.getWidth() != w || volatileImage.getHeight() != h || volatileImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) { if (volatileImage != null) { volatileImage.flush(); } volatileImage = gc.createCompatibleVolatileImage(w, h); volatileImage.setAccelerationPriority(1.0f); } // // Now paint the BufferedImage into the accelerated image // Graphics2D g = volatileImage.createGraphics(); g.drawImage(bufferedImage, 0, 0, null); g.dispose(); } while (volatileImage.contentsLost()); }