2
|
1 |
/*
|
|
2 |
* Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.awt.image;
|
|
27 |
|
|
28 |
import java.awt.Component;
|
|
29 |
import java.awt.Color;
|
|
30 |
import java.awt.SystemColor;
|
|
31 |
import java.awt.Font;
|
|
32 |
import java.awt.Graphics;
|
|
33 |
import java.awt.Graphics2D;
|
|
34 |
import java.awt.GraphicsEnvironment;
|
|
35 |
import java.awt.image.BufferedImage;
|
|
36 |
import java.awt.image.ImageProducer;
|
|
37 |
import java.awt.image.ColorModel;
|
|
38 |
import java.awt.image.WritableRaster;
|
|
39 |
import sun.java2d.SunGraphics2D;
|
|
40 |
import sun.java2d.SurfaceData;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* This is a special variant of BufferedImage that keeps a reference to
|
|
44 |
* a Component. The Component's foreground and background colors and
|
|
45 |
* default font are used as the defaults for this image.
|
|
46 |
*/
|
|
47 |
public class OffScreenImage extends BufferedImage {
|
|
48 |
|
|
49 |
protected Component c;
|
|
50 |
private OffScreenImageSource osis;
|
|
51 |
private Font defaultFont;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Constructs an OffScreenImage given a color model and tile,
|
|
55 |
* for offscreen rendering to be used with a given component.
|
|
56 |
* The component is used to obtain the foreground color, background
|
|
57 |
* color and font.
|
|
58 |
*/
|
|
59 |
public OffScreenImage(Component c, ColorModel cm, WritableRaster raster,
|
|
60 |
boolean isRasterPremultiplied)
|
|
61 |
{
|
|
62 |
super(cm, raster, isRasterPremultiplied, null);
|
|
63 |
this.c = c;
|
|
64 |
initSurface(raster.getWidth(), raster.getHeight());
|
|
65 |
}
|
|
66 |
|
|
67 |
public Graphics getGraphics() {
|
|
68 |
return createGraphics();
|
|
69 |
}
|
|
70 |
|
|
71 |
public Graphics2D createGraphics() {
|
|
72 |
if (c == null) {
|
|
73 |
GraphicsEnvironment env =
|
|
74 |
GraphicsEnvironment.getLocalGraphicsEnvironment();
|
|
75 |
return env.createGraphics(this);
|
|
76 |
}
|
|
77 |
|
|
78 |
Color bg = c.getBackground();
|
|
79 |
if (bg == null) {
|
|
80 |
bg = SystemColor.window;
|
|
81 |
}
|
|
82 |
|
|
83 |
Color fg = c.getForeground();
|
|
84 |
if (fg == null) {
|
|
85 |
fg = SystemColor.windowText;
|
|
86 |
}
|
|
87 |
|
|
88 |
Font font = c.getFont();
|
|
89 |
if (font == null) {
|
|
90 |
if (defaultFont == null) {
|
|
91 |
defaultFont = new Font("Dialog", Font.PLAIN, 12);
|
|
92 |
}
|
|
93 |
font = defaultFont;
|
|
94 |
}
|
|
95 |
|
|
96 |
return new SunGraphics2D(SurfaceData.getPrimarySurfaceData(this),
|
|
97 |
fg, bg, font);
|
|
98 |
}
|
|
99 |
|
|
100 |
private void initSurface(int width, int height) {
|
|
101 |
Graphics2D g2 = createGraphics();
|
|
102 |
try {
|
|
103 |
g2.clearRect(0, 0, width, height);
|
|
104 |
} finally {
|
|
105 |
g2.dispose();
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
public ImageProducer getSource() {
|
|
110 |
if (osis == null) {
|
|
111 |
osis = new OffScreenImageSource(this);
|
|
112 |
}
|
|
113 |
return osis;
|
|
114 |
}
|
|
115 |
}
|