01: /*
02: * @(#)ImageUtil.java 1.2 04/12/06
03: *
04: * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.awt;
10:
11: import java.awt.*;
12: import java.awt.image.*;
13:
14: public class ImageUtil {
15: static Component component = new Component() {
16: };
17: static MediaTracker tracker = new MediaTracker(component);
18: static int sid = 0;
19:
20: public static boolean waitForImage(Image image) {
21: int id;
22: synchronized (component) {
23: id = sid++;
24: }
25: tracker.addImage(image, id);
26: try {
27: tracker.waitForID(id);
28: } catch (InterruptedException e) {
29: return false;
30: }
31: tracker.removeImage(image, id);
32: return !tracker.isErrorID(id);
33: }
34:
35: public static BufferedImage makeBufferedImage(Image image) {
36: return makeBufferedImage(image, BufferedImage.TYPE_INT_RGB);
37: }
38:
39: public static BufferedImage makeBufferedImage(Image image, int type) {
40: if (!waitForImage(image)) {
41: return null;
42: }
43: BufferedImage bim = new BufferedImage(image.getWidth(null),
44: image.getHeight(null), type);
45: Graphics g = bim.createGraphics();
46: g.drawImage(image, 0, 0, null);
47: return bim;
48: }
49: }
|