001: /*
002: * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.image;
027:
028: import java.awt.AlphaComposite;
029: import java.awt.Color;
030: import java.awt.Component;
031: import java.awt.Font;
032: import java.awt.Graphics;
033: import java.awt.Graphics2D;
034: import java.awt.GraphicsConfiguration;
035: import java.awt.ImageCapabilities;
036: import java.awt.Transparency;
037: import java.awt.image.BufferedImage;
038: import java.awt.image.ColorModel;
039: import java.awt.image.ImageObserver;
040: import java.awt.image.VolatileImage;
041: import java.awt.image.WritableRaster;
042: import sun.java2d.SunGraphics2D;
043: import sun.java2d.SurfaceData;
044: import sun.java2d.SurfaceManagerFactory;
045:
046: /**
047: * This class is the base implementation of the VolatileImage
048: * abstract class. The class implements most of the standard Image
049: * methods (width, height, etc.) but delegates all surface management
050: * issues to a platform-specific VolatileSurfaceManager. When a new instance
051: * of SunVolatileImage is created, it automatically creates an
052: * appropriate VolatileSurfaceManager for the GraphicsConfiguration
053: * under which this SunVolatileImage was created.
054: */
055: public class SunVolatileImage extends VolatileImage {
056:
057: protected VolatileSurfaceManager volSurfaceManager;
058: protected Component comp;
059: private GraphicsConfiguration graphicsConfig;
060: private Font defaultFont;
061: private int width, height;
062:
063: private SunVolatileImage(Component comp,
064: GraphicsConfiguration graphicsConfig, int width,
065: int height, Object context, int transparency,
066: ImageCapabilities caps) {
067: this .comp = comp;
068: this .graphicsConfig = graphicsConfig;
069: this .width = width;
070: this .height = height;
071: if (!(transparency == Transparency.OPAQUE
072: || transparency == Transparency.BITMASK || transparency == Transparency.TRANSLUCENT)) {
073: throw new IllegalArgumentException(
074: "Unknown transparency type:" + transparency);
075: }
076: this .transparency = transparency;
077: this .volSurfaceManager = createSurfaceManager(context, caps);
078: SurfaceManager.setManager(this , volSurfaceManager);
079:
080: // post-construction initialization of the surface manager
081: volSurfaceManager.initialize();
082: // clear the background
083: volSurfaceManager.initContents();
084: }
085:
086: private SunVolatileImage(Component comp,
087: GraphicsConfiguration graphicsConfig, int width,
088: int height, Object context, ImageCapabilities caps) {
089: this (comp, graphicsConfig, width, height, context,
090: Transparency.OPAQUE, caps);
091: }
092:
093: public SunVolatileImage(Component comp, int width, int height) {
094: this (comp, width, height, null);
095: }
096:
097: public SunVolatileImage(Component comp, int width, int height,
098: Object context) {
099: this (comp, comp.getGraphicsConfiguration(), width, height,
100: context, null);
101: }
102:
103: public SunVolatileImage(GraphicsConfiguration graphicsConfig,
104: int width, int height, int transparency,
105: ImageCapabilities caps) {
106: this (null, graphicsConfig, width, height, null, transparency,
107: caps);
108: }
109:
110: public int getWidth() {
111: return width;
112: }
113:
114: public int getHeight() {
115: return height;
116: }
117:
118: public GraphicsConfiguration getGraphicsConfig() {
119: return graphicsConfig;
120: }
121:
122: public void updateGraphicsConfig() {
123: // If this VImage is associated with a Component, get an updated
124: // graphicsConfig from that component. Otherwise, keep the one
125: // that we were created with
126: if (comp != null) {
127: GraphicsConfiguration gc = comp.getGraphicsConfiguration();
128: if (gc != null) {
129: // Could potentially be null in some failure situations;
130: // better to keep the old non-null value around than to
131: // set graphicsConfig to null
132: graphicsConfig = gc;
133: }
134: }
135: }
136:
137: public Component getComponent() {
138: return comp;
139: }
140:
141: protected VolatileSurfaceManager createSurfaceManager(
142: Object context, ImageCapabilities caps) {
143: /**
144: * Platform-specific SurfaceManagerFactories will return a
145: * manager suited to acceleration on each platform. But if
146: * the user is asking for a VolatileImage from a BufferedImageGC,
147: * then we need to return the appropriate unaccelerated manager.
148: * Note: this could change in the future; if some platform would
149: * like to accelerate BIGC volatile images, then this special-casing
150: * of the BIGC graphicsConfig should live in platform-specific
151: * code instead.
152: * We do the same for a Printer Device, and if user requested an
153: * unaccelerated VolatileImage by passing the capabilities object.
154: */
155: if (graphicsConfig instanceof BufferedImageGraphicsConfig
156: || graphicsConfig instanceof sun.print.PrinterGraphicsConfig
157: || (caps != null && !caps.isAccelerated())) {
158: return new BufImgVolatileSurfaceManager(this , context);
159: }
160: return SurfaceManagerFactory.createVolatileManager(this ,
161: context);
162: }
163:
164: private Color getForeground() {
165: if (comp != null) {
166: return comp.getForeground();
167: } else {
168: return Color.black;
169: }
170: }
171:
172: private Color getBackground() {
173: if (comp != null) {
174: return comp.getBackground();
175: } else {
176: return Color.white;
177: }
178: }
179:
180: private Font getFont() {
181: if (comp != null) {
182: return comp.getFont();
183: } else {
184: if (defaultFont == null) {
185: defaultFont = new Font("Dialog", Font.PLAIN, 12);
186: }
187: return defaultFont;
188: }
189: }
190:
191: public Graphics2D createGraphics() {
192: return new SunGraphics2D(volSurfaceManager
193: .getPrimarySurfaceData(), getForeground(),
194: getBackground(), getFont());
195: }
196:
197: // Image method implementations
198: public Object getProperty(String name, ImageObserver observer) {
199: if (name == null) {
200: throw new NullPointerException(
201: "null property name is not allowed");
202: }
203: return java.awt.Image.UndefinedProperty;
204: }
205:
206: public int getWidth(ImageObserver observer) {
207: return getWidth();
208: }
209:
210: public int getHeight(ImageObserver observer) {
211: return getHeight();
212: }
213:
214: /**
215: * This method creates a BufferedImage intended for use as a "snapshot"
216: * or a backup surface.
217: */
218: public BufferedImage getBackupImage() {
219: return graphicsConfig.createCompatibleImage(getWidth(),
220: getHeight(), getTransparency());
221: }
222:
223: public BufferedImage getSnapshot() {
224: BufferedImage bi = getBackupImage();
225: Graphics2D g = bi.createGraphics();
226: g.setComposite(AlphaComposite.Src);
227: g.drawImage(this , 0, 0, null);
228: g.dispose();
229: return bi;
230: }
231:
232: public int validate(GraphicsConfiguration gc) {
233: return volSurfaceManager.validate(gc);
234: }
235:
236: public boolean contentsLost() {
237: return volSurfaceManager.contentsLost();
238: }
239:
240: public ImageCapabilities getCapabilities() {
241: return volSurfaceManager.getCapabilities(graphicsConfig);
242: }
243: }
|