1:
37:
38:
39: package ;
40:
41:
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50:
53: public class AsyncImage
54: extends Image
55: {
56:
57:
60: private class AsyncImageSource
61: implements ImageProducer
62: {
63:
67: private ImageProducer realSource;
68:
69: public void addConsumer(ImageConsumer ic)
70: {
71: startProduction(ic);
72: }
73:
74: public boolean isConsumer(ImageConsumer ic)
75: {
76: return false;
77: }
78:
79: public void removeConsumer(ImageConsumer ic)
80: {
81:
82: }
83:
84: public void requestTopDownLeftRightResend(ImageConsumer ic)
85: {
86: startProduction(ic);
87: }
88:
89: public void startProduction(ImageConsumer ic)
90: {
91: ImageProducer ip = getRealSource();
92: if (ip == null)
93: {
94: ic.setDimensions(1, 1);
95: ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
96: }
97: else
98: {
99: ip.startProduction(ic);
100: }
101: }
102:
103:
109: private ImageProducer getRealSource()
110: {
111: synchronized (AsyncImage.this)
112: {
113: ImageProducer source = realSource;
114: if (source == null)
115: {
116: Image ri = realImage;
117: if (ri != null)
118: {
119: realSource = source = ri.getSource();
120: }
121: }
122: return source;
123: }
124: }
125: }
126:
127:
130: private volatile Image realImage;
131:
132:
137: HashSet<ImageObserver> observers;
138:
139: private volatile boolean complete = false;
140:
141:
144: AsyncImage()
145: {
146: observers = new HashSet<ImageObserver>();
147: }
148:
149: public void flush()
150: {
151:
152: }
153:
154: public Graphics getGraphics()
155: {
156: Image r = realImage;
157: Graphics g = null;
158: if (r != null)
159: g = r.getGraphics();
160: return g;
161: }
162:
163: public boolean isComplete() {
164: return complete;
165: }
166:
167: public int getHeight(ImageObserver observer)
168: {
169: addObserver(observer);
170: int height = -1;
171: waitForImage(observer);
172: Image r = realImage;
173: if (r != null)
174: height = r.getHeight(observer);
175: return height;
176: }
177:
178: public Object getProperty(String name, ImageObserver observer)
179: {
180: addObserver(observer);
181: Image r = realImage;
182: Object prop = null;
183: if (r != null)
184: prop = r.getProperty(name, observer);
185: return prop;
186: }
187:
188: public ImageProducer getSource()
189: {
190: return new AsyncImageSource();
191: }
192:
193: public int getWidth(ImageObserver observer)
194: {
195: addObserver(observer);
196: int width = -1;
197: waitForImage(observer);
198: Image r = realImage;
199: if (r != null)
200: width = r.getWidth(observer);
201: return width;
202: }
203:
204: public void addObserver(ImageObserver obs)
205: {
206: if (obs != null)
207: {
208: synchronized (this)
209: {
210:
211:
212: HashSet<ImageObserver> observs = observers;
213: if (observs != null)
214: {
215: observs.add(obs);
216: }
217: }
218: }
219: }
220:
221: public boolean prepareImage(int w, int h, ImageObserver obs)
222: {
223: addObserver(obs);
224: return realImage != null;
225: }
226:
227: public int checkImage(int w, int h, ImageObserver obs)
228: {
229: addObserver(obs);
230: int flags = 0;
231: if (realImage != null)
232: flags = ImageObserver.ALLBITS | ImageObserver.WIDTH
233: | ImageObserver.HEIGHT | ImageObserver.PROPERTIES;
234: return flags;
235: }
236:
237: public Image getRealImage()
238: {
239: return realImage;
240: }
241:
242: public void setRealImage(Image im)
243: {
244: realImage = im;
245: int status = ImageObserver.HEIGHT | ImageObserver.WIDTH;
246: notifyObservers(status, 0, 0, im.getWidth(null), im.getHeight(null));
247: }
248:
249: public void notifyObservers(int status, int x, int y, int w, int h)
250: {
251: synchronized (this)
252: {
253: HashSet observs = observers;
254: if (observs != null)
255: {
256: Iterator i = observs.iterator();
257: while (i.hasNext())
258: {
259: ImageObserver obs = (ImageObserver) i.next();
260: boolean complete = obs.imageUpdate(this, status, x, y, realImage.getWidth(obs), realImage.getHeight(obs));
261: if (complete)
262: i.remove();
263: }
264: }
265: if ((status & ImageObserver.ALLBITS) != 0)
266: {
267: complete = true;
268: notifyAll();
269: }
270: }
271: }
272:
273:
280: public void waitForImage(ImageObserver observer)
281: {
282: if (!complete && observer == null)
283: {
284: synchronized (this)
285: {
286: while (! complete)
287: {
288: try
289: {
290: wait();
291: }
292: catch (InterruptedException ex)
293: {
294: Thread.currentThread().interrupt();
295: }
296: }
297: }
298: }
299: }
300: }