001: /*
002: * @(#)PPCImage.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.awt.pocketpc;
028:
029: import java.awt.Component;
030: import java.awt.Graphics;
031: import java.awt.GraphicsEnvironment;
032: import java.awt.Color;
033: import java.awt.image.ImageProducer;
034: import java.awt.image.ImageObserver;
035: import java.awt.image.ColorModel;
036: import java.awt.image.DirectColorModel;
037: import java.awt.image.BufferedImage;
038: import java.awt.image.RasterFormatException;
039: import sun.awt.image.ImageRepresentation;
040: import sun.awt.image.BufferedImagePeer;
041:
042: class PPCImage extends sun.awt.image.Image implements
043: BufferedImagePeer, PPCDrawableImage {
044: PPCGraphicsConfiguration gc;
045:
046: /* Tunnel the Offscreen image constructor to the super class. */
047: protected PPCImage(Component c, int w, int h) {
048: super (c, w, h);
049: gc = (PPCGraphicsConfiguration) c.getGraphicsConfiguration();
050: if (gc == null) {
051: gc = (PPCGraphicsConfiguration) GraphicsEnvironment
052: .getLocalGraphicsEnvironment()
053: .getDefaultScreenDevice().getDefaultConfiguration();
054: }
055: }
056:
057: /**
058: * Construct an image from an ImageProducer object.
059: */
060: public PPCImage(ImageProducer producer) {
061: super (producer);
062: gc = (PPCGraphicsConfiguration) GraphicsEnvironment
063: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
064: .getDefaultConfiguration();
065: }
066:
067: public Graphics getGraphics() {
068: Graphics g = new PPCGraphics(this );
069: initGraphics(g);
070: return g;
071: }
072:
073: public boolean isComplete() {
074: return ((getImageRep().check(null) & ImageObserver.ALLBITS) != 0);
075: }
076:
077: protected ImageRepresentation makeImageRep() {
078: return new PPCImageRepresentation(this );
079: }
080:
081: protected ImageRepresentation getImageRep() {
082: return super .getImageRep();
083: }
084:
085: // BufferedImagePeer implementation
086:
087: public int getType() {
088: return BufferedImage.TYPE_CUSTOM;
089: }
090:
091: public ColorModel getColorModel() {
092: return gc.getColorModel();
093: }
094:
095: public Object getProperty(String name) {
096: return super .getProperty(name, null);
097: }
098:
099: public BufferedImage getSubimage(int x, int y, int w, int h) {
100: if ((x + w < x) || (x + w > getWidth())) {
101: throw new RasterFormatException(
102: "(x + width) is outside raster");
103: }
104: if ((y + h < y) || (y + h > getHeight())) {
105: throw new RasterFormatException(
106: "(y + height) is outside raster");
107: }
108: return PPCToolkit.createBufferedImage(new PPCSubImage(this , x,
109: y, w, h));
110: }
111:
112: public String[] getPropertyNames() {
113: return null;
114: }
115:
116: public int getRGB(int x, int y) {
117: return ((PPCImageRepresentation) getImageRep()).getRGB(
118: ColorModel.getRGBdefault(), x, y);
119: }
120:
121: public int[] getRGB(int startX, int startY, int w, int h,
122: int[] rgbArray, int offset, int scansize) {
123: if (null == rgbArray)
124: rgbArray = new int[offset + h * scansize];
125: else if (rgbArray.length < offset + h * scansize)
126: throw new IllegalArgumentException(
127: "rgbArray is not large enough to store all the values");
128: ((PPCImageRepresentation) getImageRep()).getRGBs(ColorModel
129: .getRGBdefault(), startX, startY, w, h, rgbArray,
130: offset, scansize);
131: return rgbArray;
132: }
133:
134: /** The color model used for setRGB calls. We don't use the default RGB
135: color model because this has alpha and BufferedImages don't have an
136: alpha channel. This may be changed in the future if we add alpha
137: channel rendering to the Graphics object for BufferedImages.
138: see bug 4732134 */
139:
140: private static final ColorModel COLOR_MODEL_FOR_SET_RGB = new DirectColorModel(
141: 24, 0xff0000, 0xff00, 0xff);
142:
143: public void setRGB(int x, int y, int rgb) {
144: getImageRep().setPixels(x, y, 1, 1, COLOR_MODEL_FOR_SET_RGB,
145: new int[] { rgb }, 0, 0);
146: }
147:
148: public void setRGB(int startX, int startY, int w, int h,
149: int[] rgbArray, int offset, int scansize) {
150: getImageRep().setPixels(startX, startY, w, h,
151: COLOR_MODEL_FOR_SET_RGB, rgbArray, offset, scansize);
152: }
153:
154: // PPCDrawableImage implementation
155:
156: public boolean draw(PPCGraphics g, int x, int y,
157: ImageObserver observer) {
158: if (hasError()) {
159: if (observer != null) {
160: observer.imageUpdate(this , ImageObserver.ERROR
161: | ImageObserver.ABORT, -1, -1, -1, -1);
162: }
163: return false;
164: }
165: return getImageRep().drawImage(g, x, y, null, observer);
166: }
167:
168: public boolean draw(PPCGraphics g, int x, int y, int width,
169: int height, ImageObserver observer) {
170: if (width == 0 || height == 0) {
171: return true;
172: }
173: if (hasError()) {
174: if (observer != null) {
175: observer.imageUpdate(this , ImageObserver.ERROR
176: | ImageObserver.ABORT, -1, -1, -1, -1);
177: }
178: return false;
179: }
180: return getImageRep().drawScaledImage(g, x, y, width, height,
181: null, observer);
182: }
183:
184: public boolean draw(PPCGraphics g, int x, int y, Color bg,
185: ImageObserver observer) {
186: if (hasError()) {
187: if (observer != null) {
188: observer.imageUpdate(this , ImageObserver.ERROR
189: | ImageObserver.ABORT, -1, -1, -1, -1);
190: }
191: return false;
192: }
193: return getImageRep().drawImage(g, x, y, bg, observer);
194: }
195:
196: public boolean draw(PPCGraphics g, int x, int y, int width,
197: int height, Color bg, ImageObserver observer) {
198: if (width == 0 || height == 0) {
199: return true;
200: }
201: if (hasError()) {
202: if (observer != null) {
203: observer.imageUpdate(this , ImageObserver.ERROR
204: | ImageObserver.ABORT, -1, -1, -1, -1);
205: }
206: return false;
207: }
208: return getImageRep().drawScaledImage(g, x, y, width, height,
209: bg, observer);
210: }
211:
212: public boolean draw(PPCGraphics g, int dx1, int dy1, int dx2,
213: int dy2, int sx1, int sy1, int sx2, int sy2,
214: ImageObserver observer) {
215: if (dx1 == dx2 || dy1 == dy2) {
216: return true;
217: }
218: if (hasError()) {
219: if (observer != null) {
220: observer.imageUpdate(this , ImageObserver.ERROR
221: | ImageObserver.ABORT, -1, -1, -1, -1);
222: }
223: return false;
224: }
225: return getImageRep().drawStretchImage(g, dx1, dy1, dx2, dy2,
226: sx1, sy1, sx2, sy2, null, observer);
227: }
228:
229: public boolean draw(PPCGraphics g, int dx1, int dy1, int dx2,
230: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
231: ImageObserver observer) {
232: if (dx1 == dx2 || dy1 == dy2) {
233: return true;
234: }
235: if (hasError()) {
236: if (observer != null) {
237: observer.imageUpdate(this , ImageObserver.ERROR
238: | ImageObserver.ABORT, -1, -1, -1, -1);
239: }
240: return false;
241: }
242: return getImageRep().drawStretchImage(g, dx1, dy1, dx2, dy2,
243: sx1, sy1, sx2, sy2, bgcolor, observer);
244: }
245: }
|