001: /*
002: *
003: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package tests.volatileImage;
027:
028: import java.awt.*;
029: import java.awt.image.*;
030:
031: public class TestVolatileGC extends Frame {
032: VolatileImage volImage;
033: Image origImage = null;
034:
035: TestVolatileGC() {
036: MediaTracker mt = new MediaTracker(this );
037: origImage = getToolkit().getImage("duke.gif");
038: try {
039: mt.addImage(origImage, 0);
040: mt.waitForAll();
041: } catch (Exception e) {
042: e.printStackTrace();
043: }
044: }
045:
046: public void paint(Graphics g) {
047: volImage = drawVolatileImage((Graphics2D) g, volImage, 0, 0,
048: origImage);
049: }
050:
051: // This method draws a volatile image and returns it or possibly a
052: // newly created volatile image object. Subsequent calls to this method
053: // should always use the returned volatile image.
054: // If the contents of the image is lost, it is recreated using orig.
055: // img may be null, in which case a new volatile image is created.
056: public VolatileImage drawVolatileImage(Graphics2D g,
057: VolatileImage img, int x, int y, Image orig) {
058: final int MAX_TRIES = 100;
059: for (int i = 0; i < MAX_TRIES; i++) {
060: if (img != null) {
061: // Draw the volatile image
062: g.drawImage(img, x, y, null);
063:
064: // Check if it is still valid
065: if (!img.contentsLost()) {
066: return img;
067: }
068: } else {
069: // Create the volatile image
070: img = g.getDeviceConfiguration()
071: .createCompatibleVolatileImage(
072: orig.getWidth(null),
073: orig.getHeight(null));
074: }
075:
076: // Determine how to fix the volatile image
077: switch (img.validate(g.getDeviceConfiguration())) {
078: case VolatileImage.IMAGE_INCOMPATIBLE:
079: // Create a new volatile image object;
080: // this could happen if the component was moved to another
081: // device
082: img.flush();
083: img = g.getDeviceConfiguration()
084: .createCompatibleVolatileImage(
085: orig.getWidth(null),
086: orig.getHeight(null));
087: case VolatileImage.IMAGE_OK:
088: case VolatileImage.IMAGE_RESTORED:
089: // Copy the original image to accelerated image memory
090: Graphics2D gc = (Graphics2D) img.createGraphics();
091: gc.drawImage(orig, x, y, null);
092: gc.dispose();
093: break;
094: }
095: }
096:
097: // The image failed to be drawn after MAX_TRIES;
098: // draw with the non-accelerated image
099: System.out
100: .println("Failed to draw accelerated image - falling back "
101: + " to non-accelerated image!");
102: g.drawImage(orig, x, y, null);
103: return img;
104: }
105:
106: public static void main(String args[]) {
107: TestVolatileGC tv = new TestVolatileGC();
108: tv.setSize(400, 400);
109: tv.setVisible(true);
110: }
111: }
|