001: /*
002: * Copyright 1996-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.Component;
029: import java.awt.Color;
030: import java.awt.SystemColor;
031: import java.awt.Font;
032: import java.awt.Graphics;
033: import java.awt.Graphics2D;
034: import java.awt.GraphicsEnvironment;
035: import java.awt.image.BufferedImage;
036: import java.awt.image.ImageProducer;
037: import java.awt.image.ColorModel;
038: import java.awt.image.WritableRaster;
039: import sun.java2d.SunGraphics2D;
040: import sun.java2d.SurfaceData;
041:
042: /**
043: * This is a special variant of BufferedImage that keeps a reference to
044: * a Component. The Component's foreground and background colors and
045: * default font are used as the defaults for this image.
046: */
047: public class OffScreenImage extends BufferedImage {
048:
049: protected Component c;
050: private OffScreenImageSource osis;
051: private Font defaultFont;
052:
053: /**
054: * Constructs an OffScreenImage given a color model and tile,
055: * for offscreen rendering to be used with a given component.
056: * The component is used to obtain the foreground color, background
057: * color and font.
058: */
059: public OffScreenImage(Component c, ColorModel cm,
060: WritableRaster raster, boolean isRasterPremultiplied) {
061: super (cm, raster, isRasterPremultiplied, null);
062: this .c = c;
063: initSurface(raster.getWidth(), raster.getHeight());
064: }
065:
066: public Graphics getGraphics() {
067: return createGraphics();
068: }
069:
070: public Graphics2D createGraphics() {
071: if (c == null) {
072: GraphicsEnvironment env = GraphicsEnvironment
073: .getLocalGraphicsEnvironment();
074: return env.createGraphics(this );
075: }
076:
077: Color bg = c.getBackground();
078: if (bg == null) {
079: bg = SystemColor.window;
080: }
081:
082: Color fg = c.getForeground();
083: if (fg == null) {
084: fg = SystemColor.windowText;
085: }
086:
087: Font font = c.getFont();
088: if (font == null) {
089: if (defaultFont == null) {
090: defaultFont = new Font("Dialog", Font.PLAIN, 12);
091: }
092: font = defaultFont;
093: }
094:
095: return new SunGraphics2D(SurfaceData
096: .getPrimarySurfaceData(this ), fg, bg, font);
097: }
098:
099: private void initSurface(int width, int height) {
100: Graphics2D g2 = createGraphics();
101: try {
102: g2.clearRect(0, 0, width, height);
103: } finally {
104: g2.dispose();
105: }
106: }
107:
108: public ImageProducer getSource() {
109: if (osis == null) {
110: osis = new OffScreenImageSource(this);
111: }
112: return osis;
113: }
114: }
|