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 TestVolatileComponent extends Frame {
032: VolatileImage volImage;
033: Image origImage = null;
034:
035: TestVolatileComponent() {
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: // This one uses the constructor on component.
057: public VolatileImage drawVolatileImage(Graphics2D g,
058: VolatileImage img, int x, int y, Image orig) {
059: final int MAX_TRIES = 100;
060: for (int i = 0; i < MAX_TRIES; i++) {
061: if (img != null) {
062: // Draw the volatile image
063: g.drawImage(img, x, y, null);
064:
065: // Check if it is still valid
066: if (!img.contentsLost()) {
067: return img;
068: }
069: } else {
070: // Create the volatile image
071: img = createVolatileImage(orig.getWidth(null), orig
072: .getHeight(null));
073: }
074:
075: // Determine how to fix the volatile image
076: switch (img.validate(g.getDeviceConfiguration())) {
077: case VolatileImage.IMAGE_INCOMPATIBLE:
078: // Create a new volatile image object;
079: // this could happen if the component was moved to another
080: // device
081: img.flush();
082: img = createVolatileImage(orig.getWidth(null), orig
083: .getHeight(null));
084: case VolatileImage.IMAGE_OK:
085: case VolatileImage.IMAGE_RESTORED:
086: // Copy the original image to accelerated image memory
087: Graphics2D gc = (Graphics2D) img.createGraphics();
088: gc.drawImage(orig, x, y, null);
089: gc.dispose();
090: break;
091: }
092: }
093:
094: // The image failed to be drawn after MAX_TRIES;
095: // draw with the non-accelerated image
096: System.out
097: .println("Failed to draw accelerated image - falling back "
098: + " to non-accelerated image!");
099: g.drawImage(orig, x, y, null);
100: return img;
101: }
102:
103: public static void main(String args[]) {
104: TestVolatileComponent tv = new TestVolatileComponent();
105: tv.setSize(400, 400);
106: tv.setVisible(true);
107: }
108: }
|