001: /*
002: * @(#)ImageCanvas.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.awt;
010:
011: import java.awt.*;
012: import java.awt.image.*;
013: import java.io.*;
014: import java.net.URL;
015:
016: /**
017: * ImageCanvas class is a canvas which displays Image without
018: * specifying width and height.
019: *
020: * @author Toyokazu Tomatsu
021: * @version 1.1
022: */
023: public class ImageCanvas extends Canvas {
024: /**
025: * @serial
026: */
027: Image image;
028:
029: /**
030: * @serial
031: */
032: int width = -1;
033:
034: /**
035: * @serial
036: */
037: int height = -1;
038:
039: /**
040: * @serial
041: */
042: double rate_x = 1f;
043:
044: /**
045: * @serial
046: */
047: double rate_y = 1f;
048:
049: /**
050: * @serial
051: */
052: private boolean completed = false;
053:
054: private boolean error = false;
055:
056: /**
057: * @serial
058: */
059: private int top;
060:
061: /**
062: * @serial
063: */
064: private int bottom;
065:
066: public ImageCanvas(Image image) {
067: this (image, -1, -1);
068: }
069:
070: public ImageCanvas(URL url) {
071: this (url, -1, -1);
072: }
073:
074: public ImageCanvas(String file) throws FileNotFoundException {
075: this (file, -1, -1);
076: }
077:
078: public ImageCanvas(Image image, double rate_x, double rate_y) {
079: this (image);
080: this .rate_x = rate_x;
081: this .rate_y = rate_y;
082: }
083:
084: public ImageCanvas(URL url, double rate_x, double rate_y) {
085: this (url);
086: this .rate_x = rate_x;
087: this .rate_y = rate_y;
088: }
089:
090: public ImageCanvas(String file, double rate_x, double rate_y)
091: throws FileNotFoundException {
092: this (file);
093: this .rate_x = rate_x;
094: this .rate_y = rate_y;
095: }
096:
097: public ImageCanvas(Image image, int width, int height) {
098: this .image = image;
099: this .width = width;
100: this .height = height;
101: completed = (image.getWidth(this ) >= 0 && image.getHeight(this ) >= 0);
102: }
103:
104: public ImageCanvas(URL url, int width, int height) {
105: this .image = Toolkit.getDefaultToolkit().getImage(url);
106: this .width = width;
107: this .height = height;
108: completed = (image.getWidth(this ) >= 0 && image.getHeight(this ) >= 0);
109: }
110:
111: public ImageCanvas(String file, int width, int height)
112: throws FileNotFoundException {
113: if (new File(file).exists()) {
114: this .image = Toolkit.getDefaultToolkit().getImage(file);
115: this .width = width;
116: this .height = height;
117: } else {
118: throw new FileNotFoundException(file);
119: }
120: completed = (image.getWidth(this ) >= 0 && image.getHeight(this ) >= 0);
121: }
122:
123: public synchronized Dimension getPreferredSize() {
124: if (width >= 0 && height >= 0) {
125: return new Dimension(width, height);
126: }
127: int w = image.getWidth(this );
128: int h = image.getHeight(this );
129: while (!error && (w < 0 || h < 0)) {
130: try {
131: wait(1000);
132: } catch (InterruptedException e) {
133: }
134: w = image.getWidth(this );
135: h = image.getHeight(this );
136: }
137: if (rate_x == 1f && rate_y == 1f) {
138: return new Dimension(w, h);
139: } else {
140: w = (int) Math.round(w * rate_x);
141: h = (int) Math.round(h * rate_y);
142: return new Dimension(w, h);
143: }
144: }
145:
146: public synchronized void sync() {
147: while (!completed && !error) {
148: try {
149: wait(1000);
150: } catch (InterruptedException e) {
151: }
152: }
153: }
154:
155: public synchronized boolean imageUpdate(Image img, int flags,
156: int x, int y, int w, int h) {
157: if ((flags & ImageObserver.ERROR) != 0) {
158: error = true;
159: return true;
160: }
161: boolean b = super .imageUpdate(img, flags, x, y, w, h);
162: bottom = y;
163: if (!b)
164: completed = true;
165: notifyAll();
166: return b;
167: }
168:
169: public void paint(Graphics g) {
170: int w = getSize().width;
171: int h = getSize().height;
172: if (!completed) {
173: g.setClip(0, top, w, bottom - top);
174: } else {
175: g.setClip(0, 0, w, h);
176: }
177: if (width >= 0 && height >= 0) {
178: g.drawImage(image, 0, 0, width, height, this );
179: } else if (rate_x == 1f && rate_y == 1f) {
180: g.drawImage(image, 0, 0, this );
181: } else {
182: w = getPreferredSize().width;
183: h = getPreferredSize().height;
184: g.drawImage(image, 0, 0, w, h, this );
185: }
186: top = bottom;
187: }
188:
189: public void update(Graphics g) {
190: paint(g);
191: }
192:
193: public boolean isError() {
194: return error;
195: }
196: }
|