28 import java.awt.AWTException;
29 import java.awt.Component;
30 import java.awt.Graphics2D;
31 import java.awt.GraphicsConfiguration;
32 import java.awt.GraphicsDevice;
33 import java.awt.ImageCapabilities;
34 import java.awt.Rectangle;
35 import java.awt.Transparency;
36 import java.awt.geom.AffineTransform;
37 import java.awt.image.BufferedImage;
38 import java.awt.image.ColorModel;
39 import java.awt.image.DirectColorModel;
40 import java.awt.image.Raster;
41 import java.awt.image.VolatileImage;
42 import java.awt.image.WritableRaster;
43
44 public class BufferedImageGraphicsConfig
45 extends GraphicsConfiguration
46 {
47 private static final int numconfigs = BufferedImage.TYPE_BYTE_BINARY;
48 private static BufferedImageGraphicsConfig configs[] =
49 new BufferedImageGraphicsConfig[numconfigs];
50
51 public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg) {
52 BufferedImageGraphicsConfig ret;
53 int type = bImg.getType();
54 if (type > 0 && type < numconfigs) {
55 ret = configs[type];
56 if (ret != null) {
57 return ret;
58 }
59 }
60 ret = new BufferedImageGraphicsConfig(bImg, null);
61 if (type > 0 && type < numconfigs) {
62 configs[type] = ret;
63 }
64 return ret;
65 }
66
67 GraphicsDevice gd;
68 ColorModel model;
69 Raster raster;
70
71 public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp) {
72 if (comp == null) {
73 this.gd = new BufferedImageDevice(this);
74 } else {
75 Graphics2D g2d = (Graphics2D)comp.getGraphics();
76 this.gd = g2d.getDeviceConfiguration().getDevice();
77 }
78 this.model = bufImg.getColorModel();
79 this.raster = bufImg.getRaster().createCompatibleWritableRaster(1, 1);
80 }
81
82 /**
83 * Return the graphics device associated with this configuration.
84 */
85 public GraphicsDevice getDevice() {
86 return gd;
87 }
88
89 /**
90 * Returns a BufferedImage with channel layout and color model
91 * compatible with this graphics configuration. This method
92 * has nothing to do with memory-mapping
93 * a device. This BufferedImage has
94 * a layout and color model
95 * that is closest to this native device configuration and thus
96 * can be optimally blitted to this device.
97 */
98 public BufferedImage createCompatibleImage(int width, int height) {
99 WritableRaster wr = raster.createCompatibleWritableRaster(width, height);
121 return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
122 case Transparency.BITMASK:
123 return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
124 case Transparency.TRANSLUCENT:
125 return ColorModel.getRGBdefault();
126 default:
127 return null;
128 }
129 }
130
131 /**
132 * Returns the default Transform for this configuration. This
133 * Transform is typically the Identity transform for most normal
134 * screens. Device coordinates for screen and printer devices will
135 * have the origin in the upper left-hand corner of the target region of
136 * the device, with X coordinates
137 * increasing to the right and Y coordinates increasing downwards.
138 * For image buffers, this Transform will be the Identity transform.
139 */
140 public AffineTransform getDefaultTransform() {
141 return new AffineTransform();
142 }
143
144 /**
145 *
146 * Returns a Transform that can be composed with the default Transform
147 * of a Graphics2D so that 72 units in user space will equal 1 inch
148 * in device space.
149 * Given a Graphics2D, g, one can reset the transformation to create
150 * such a mapping by using the following pseudocode:
151 * <pre>
152 * GraphicsConfiguration gc = g.getGraphicsConfiguration();
153 *
154 * g.setTransform(gc.getDefaultTransform());
155 * g.transform(gc.getNormalizingTransform());
156 * </pre>
157 * Note that sometimes this Transform will be identity (e.g. for
158 * printers or metafile output) and that this Transform is only
159 * as accurate as the information supplied by the underlying system.
160 * For image buffers, this Transform will be the Identity transform,
161 * since there is no valid distance measurement.
|
28 import java.awt.AWTException;
29 import java.awt.Component;
30 import java.awt.Graphics2D;
31 import java.awt.GraphicsConfiguration;
32 import java.awt.GraphicsDevice;
33 import java.awt.ImageCapabilities;
34 import java.awt.Rectangle;
35 import java.awt.Transparency;
36 import java.awt.geom.AffineTransform;
37 import java.awt.image.BufferedImage;
38 import java.awt.image.ColorModel;
39 import java.awt.image.DirectColorModel;
40 import java.awt.image.Raster;
41 import java.awt.image.VolatileImage;
42 import java.awt.image.WritableRaster;
43
44 public class BufferedImageGraphicsConfig
45 extends GraphicsConfiguration
46 {
47 private static final int numconfigs = BufferedImage.TYPE_BYTE_BINARY;
48 private static BufferedImageGraphicsConfig standardConfigs[] =
49 new BufferedImageGraphicsConfig[numconfigs];
50 private static BufferedImageGraphicsConfig scaledConfigs[] =
51 new BufferedImageGraphicsConfig[numconfigs];
52
53 public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg) {
54 return getConfig(bImg, 1, 1);
55 }
56
57 public static BufferedImageGraphicsConfig getConfig(BufferedImage bImg,
58 double scaleX,
59 double scaleY)
60 {
61 BufferedImageGraphicsConfig ret;
62 int type = bImg.getType();
63
64 BufferedImageGraphicsConfig[] configs = (scaleX == 1 && scaleY == 1)
65 ? standardConfigs : scaledConfigs;
66
67 if (type > 0 && type < numconfigs) {
68 ret = configs[type];
69 if (ret != null && ret.scaleX == scaleX && ret.scaleY == scaleY) {
70 return ret;
71 }
72 }
73 ret = new BufferedImageGraphicsConfig(bImg, null, scaleX, scaleY);
74 if (type > 0 && type < numconfigs) {
75 configs[type] = ret;
76 }
77 return ret;
78 }
79
80 GraphicsDevice gd;
81 ColorModel model;
82 Raster raster;
83 private final double scaleX;
84 private final double scaleY;
85
86 public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp) {
87 this(bufImg, comp, 1, 1);
88 }
89
90 public BufferedImageGraphicsConfig(BufferedImage bufImg, Component comp,
91 double scaleX, double scaleY)
92 {
93 if (comp == null) {
94 this.gd = new BufferedImageDevice(this);
95 } else {
96 Graphics2D g2d = (Graphics2D)comp.getGraphics();
97 this.gd = g2d.getDeviceConfiguration().getDevice();
98 }
99 this.model = bufImg.getColorModel();
100 this.raster = bufImg.getRaster().createCompatibleWritableRaster(1, 1);
101 this.scaleX = scaleX;
102 this.scaleY = scaleY;
103 }
104
105 /**
106 * Return the graphics device associated with this configuration.
107 */
108 public GraphicsDevice getDevice() {
109 return gd;
110 }
111
112 /**
113 * Returns a BufferedImage with channel layout and color model
114 * compatible with this graphics configuration. This method
115 * has nothing to do with memory-mapping
116 * a device. This BufferedImage has
117 * a layout and color model
118 * that is closest to this native device configuration and thus
119 * can be optimally blitted to this device.
120 */
121 public BufferedImage createCompatibleImage(int width, int height) {
122 WritableRaster wr = raster.createCompatibleWritableRaster(width, height);
144 return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
145 case Transparency.BITMASK:
146 return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
147 case Transparency.TRANSLUCENT:
148 return ColorModel.getRGBdefault();
149 default:
150 return null;
151 }
152 }
153
154 /**
155 * Returns the default Transform for this configuration. This
156 * Transform is typically the Identity transform for most normal
157 * screens. Device coordinates for screen and printer devices will
158 * have the origin in the upper left-hand corner of the target region of
159 * the device, with X coordinates
160 * increasing to the right and Y coordinates increasing downwards.
161 * For image buffers, this Transform will be the Identity transform.
162 */
163 public AffineTransform getDefaultTransform() {
164 return AffineTransform.getScaleInstance(scaleX, scaleY);
165 }
166
167 /**
168 *
169 * Returns a Transform that can be composed with the default Transform
170 * of a Graphics2D so that 72 units in user space will equal 1 inch
171 * in device space.
172 * Given a Graphics2D, g, one can reset the transformation to create
173 * such a mapping by using the following pseudocode:
174 * <pre>
175 * GraphicsConfiguration gc = g.getGraphicsConfiguration();
176 *
177 * g.setTransform(gc.getDefaultTransform());
178 * g.transform(gc.getNormalizingTransform());
179 * </pre>
180 * Note that sometimes this Transform will be identity (e.g. for
181 * printers or metafile output) and that this Transform is only
182 * as accurate as the information supplied by the underlying system.
183 * For image buffers, this Transform will be the Identity transform,
184 * since there is no valid distance measurement.
|