001: /*
002: * @(#)PPCSubImage.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 sun.awt.image.BufferedImagePeer;
030: import java.awt.Color;
031: import java.awt.Image;
032: import java.awt.Graphics;
033: import java.awt.image.ColorModel;
034: import java.awt.image.ImageObserver;
035: import java.awt.image.ImageProducer;
036: import java.awt.image.FilteredImageSource;
037: import java.awt.image.CropImageFilter;
038: import java.awt.image.BufferedImage;
039: import java.awt.image.RasterFormatException;
040:
041: /** An image which represents an area of another image. It shares the same
042: pixel storage as the source. */
043:
044: class PPCSubImage extends Image implements BufferedImagePeer,
045: PPCDrawableImage {
046: /** The source image for which this is a sub image of. */
047: private PPCImage sourceImage;
048:
049: /** The area of the source image this is a sub image of. */
050: private int x, y, width, height;
051:
052: /** The image producer. This is just a CropImageFilter on the origional
053: image. */
054: private ImageProducer producer;
055:
056: PPCSubImage(PPCImage sourceImage, int x, int y, int width,
057: int height) {
058: this .sourceImage = sourceImage;
059: this .x = x;
060: this .y = y;
061: this .width = width;
062: this .height = height;
063: }
064:
065: public int getWidth(ImageObserver observer) {
066: return width;
067: }
068:
069: public int getWidth() {
070: return width;
071: }
072:
073: public int getHeight(ImageObserver observer) {
074: return height;
075: }
076:
077: public int getHeight() {
078: return height;
079: }
080:
081: public synchronized ImageProducer getSource() {
082: if (producer == null)
083: producer = new FilteredImageSource(sourceImage.getSource(),
084: new CropImageFilter(x, y, width, height));
085: return producer;
086: }
087:
088: public Graphics getGraphics() {
089: Graphics g = sourceImage.getGraphics();
090: g.translate(x, y);
091: g.clipRect(0, 0, width, height);
092: return g;
093: }
094:
095: public Object getProperty(String name, ImageObserver observer) {
096: return sourceImage.getProperty(name, observer);
097: }
098:
099: public boolean isComplete() {
100: return sourceImage.isComplete();
101: }
102:
103: public void flush() {
104: }
105:
106: // BufferedImagePeer implementation
107:
108: public int getType() {
109: return sourceImage.getType();
110: }
111:
112: public ColorModel getColorModel() {
113: return sourceImage.getColorModel();
114: }
115:
116: public int getRGB(int x, int y) {
117: return sourceImage.getRGB(x + this .x, y + this .y);
118: }
119:
120: public int[] getRGB(int startX, int startY, int w, int h,
121: int[] rgbArray, int offset, int scansize) {
122: return sourceImage.getRGB(startX + this .x, startY + this .y, w,
123: h, rgbArray, offset, scansize);
124: }
125:
126: public void setRGB(int x, int y, int rgb) {
127: sourceImage.setRGB(x + this .x, y + this .y, rgb);
128: }
129:
130: public void setRGB(int startX, int startY, int w, int h,
131: int[] rgbArray, int offset, int scansize) {
132: sourceImage.setRGB(startX + this .x, startY + this .y, w, h,
133: rgbArray, offset, scansize);
134: }
135:
136: public Object getProperty(String name) {
137: return sourceImage.getProperty(name);
138: }
139:
140: public BufferedImage getSubimage(int x, int y, int w, int h) {
141: if ((x + w < this .x) || (x + w > this .x + this .width)) {
142: throw new RasterFormatException(
143: "(x + width) is outside raster");
144: }
145: if ((y + h < this .y) || (y + h > this .y + this .height)) {
146: throw new RasterFormatException(
147: "(y + height) is outside raster");
148: }
149: return PPCToolkit.createBufferedImage(new PPCSubImage(
150: sourceImage, x + this .x, y + this .y, width, height));
151: }
152:
153: public String[] getPropertyNames() {
154: return sourceImage.getPropertyNames();
155: }
156:
157: public boolean draw(PPCGraphics g, int x, int y,
158: ImageObserver observer) {
159: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
160: this .y, this .x + width, this .y + height, observer);
161: }
162:
163: public boolean draw(PPCGraphics g, int x, int y, int width,
164: int height, ImageObserver observer) {
165: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
166: this .y, this .x + this .width, this .y + this .height,
167: observer);
168: }
169:
170: public boolean draw(PPCGraphics g, int x, int y, Color bg,
171: ImageObserver observer) {
172: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
173: this .y, this .x + width, this .y + height, bg, observer);
174: }
175:
176: public boolean draw(PPCGraphics g, int x, int y, int width,
177: int height, Color bg, ImageObserver observer) {
178: return sourceImage.draw(g, x, y, x + width, y + height, this .x,
179: this .y, this .x + this .width, this .y + this .height, bg,
180: observer);
181: }
182:
183: public boolean draw(PPCGraphics g, int dx1, int dy1, int dx2,
184: int dy2, int sx1, int sy1, int sx2, int sy2,
185: ImageObserver observer) {
186: return sourceImage.draw(g, dx1, dy1, dx2, dy2, sx1 + this .x,
187: sy1 + this .y, sx2 + this .x, sy2 + this .y, observer);
188: }
189:
190: public boolean draw(PPCGraphics g, int dx1, int dy1, int dx2,
191: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
192: ImageObserver observer) {
193: return sourceImage.draw(g, dx1, dy1, dx2, dy2, sx1 + this.x,
194: sy1 + this.y, sx2 + this.x, sy2 + this.y, bgcolor,
195: observer);
196: }
197: }
|