Here you can find the source of drawVolatileImage(Graphics2D g, VolatileImage volatileImage, int x, int y, Image orig)
Parameter | Description |
---|---|
g | The destination for drawing. |
volatileImage | The image that will be drawn. Might be null, in which case a new volatile image is created. |
x | Horizontal position for drawing the image. |
y | Vertical position for drawing the image. |
orig | The original image that will be converted to a VolatileImage, if volatileImage is null, or if volatileImage is not valid anymore. |
public static VolatileImage drawVolatileImage(Graphics2D g, VolatileImage volatileImage, int x, int y, Image orig)
//package com.java2s; import java.awt.*; import java.awt.image.*; public class Main { /**//from w w w . ja va2s .c o m * From The Java Developers Almanac 1.4 <br> * e674. Creating and Drawing an Accelerated Image * This method draws a volatile image and returns it, or possibly a * newly created volatile image object. Subsequent calls to this method * should always use the returned volatile image. * If the contents of the image is lost, it is recreated using orig. * img may be null, in which case a new volatile image is created. * @param g The destination for drawing. * @param volatileImage The image that will be drawn. Might be null, in * which case a new volatile image is created. * @param x Horizontal position for drawing the image. * @param y Vertical position for drawing the image. * @return Returns a VolatileImage if one has been created, null otherwise. * Pass this VolatileImage to subsequent calls of drawVolatileImage. * @param orig The original image that will be converted to a VolatileImage, * if volatileImage is null, or if volatileImage is not valid anymore. */ public static VolatileImage drawVolatileImage(Graphics2D g, VolatileImage volatileImage, int x, int y, Image orig) { final boolean VERBOSE = false; final int MAX_TRIES = 5; for (int i = 0; i < MAX_TRIES; i++) { if (VERBOSE) { System.out.println("try " + (i + 1)); } if (volatileImage == null) { // Create the volatile image final int imageWidth = orig.getWidth(null); final int imageHeight = orig.getHeight(null); volatileImage = g.getDeviceConfiguration().createCompatibleVolatileImage(imageWidth, imageHeight); // Copy the original image to accelerated image memory Graphics2D gc = (Graphics2D) volatileImage.createGraphics(); gc.drawImage(orig, 0, 0, null); gc.dispose(); if (VERBOSE) { System.out.println("volatile image was not created before, created new volatile image of size: " + imageWidth + " x " + imageHeight); } } if (volatileImage != null) { // Draw the volatile image g.drawImage(volatileImage, x, y, null); // Check if it is still valid if (!volatileImage.contentsLost()) { if (VERBOSE) { System.out.println( "drawn volatile image, volatile image still valid, exiting drawVolatileImage"); } return volatileImage; } if (VERBOSE) { System.out.println("drawn volatile image, image not valid"); } } // Determine how to fix the volatile image switch (volatileImage.validate(g.getDeviceConfiguration())) { case VolatileImage.IMAGE_OK: // This should not happen if (VERBOSE) { System.out.println("VolatileImage.validate returned IMAGE_OK"); } break; case VolatileImage.IMAGE_INCOMPATIBLE: // Create a new volatile image object; // this could happen if the component was moved to another device volatileImage.flush(); volatileImage = g.getDeviceConfiguration().createCompatibleVolatileImage(orig.getWidth(null), orig.getHeight(null)); if (VERBOSE) { System.out.println("VolatileImage.validate returned IMAGE_INCOMPATIBLE"); } case VolatileImage.IMAGE_RESTORED: // Copy the original image to accelerated image memory Graphics2D gc = (Graphics2D) volatileImage.createGraphics(); gc.drawImage(orig, 0, 0, null); gc.dispose(); if (VERBOSE) { System.out.println("VolatileImage.validate returned IMAGE_RESTORED"); } break; } } // The image failed to be drawn after MAX_TRIES; // draw with the non-accelerated image if (VERBOSE) { System.out.println("Could not draw volatile image. Will draw normal image."); } g.drawImage(orig, x, y, null); return volatileImage; } }