Debian Codegraph @

src/main/java/org/freehep/util/images/TempImageHandler.java

raw
1 package org.freehep.util.images;
2
3 import java.awt.Color;
4 import java.awt.Cursor;
5 import java.awt.Dimension;
6 import java.awt.Graphics2D;
7 import java.awt.Image;
8 import java.awt.Point;
9 import java.awt.Toolkit;
10 import java.awt.image.BufferedImage;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.Hashtable;
15
16 import javax.swing.Icon;
17 import javax.swing.ImageIcon;
18
19 public class TempImageHandler {
20
21 private static Image brokenImage;
22 private static Cursor brokenCursor;
23 private static String[] imageExtensions = { ".gif", ".png" };
24 private static Hashtable imageCache = new Hashtable();
25 private static Hashtable cursorCache = new Hashtable();
26 private static Hashtable iconCache = new Hashtable();
27
28 // only static methods
29 protected TempImageHandler() {
30 }
31
32 static {
33 Toolkit toolkit = Toolkit.getDefaultToolkit();
34
35 byte[] brokenImageBytes = getImageBytes("BrokenImage.gif",
36 TempImageHandler.class);
37 if (brokenImageBytes != null) {
38 brokenImage = toolkit.createImage(brokenImageBytes);
39 }
40
41 if (brokenImage == null) {
42 throw new RuntimeException("Could not load BrokenIcon");
43 }
44
45 byte[] brokenCursorBytes = getImageBytes("BrokenCursor.gif",
46 TempImageHandler.class);
47 if (brokenCursorBytes != null) {
48 Image brokenCursorImage = toolkit.createImage(brokenCursorBytes);
49 if (brokenCursorImage != null) {
50 brokenCursor = toolkit.createCustomCursor(brokenCursorImage,
51 new Point(0,0),
52 "BrokenCursor");
53 }
54 }
55
56 if (brokenCursor == null) {
57 throw new RuntimeException("Could not load BrokenCursor");
58 }
59 }
60
61 public static Image getImage(String name, Class clazz) {
62 Image image = (Image)imageCache.get(name);
63 if (image == null) {
64 Toolkit toolkit = Toolkit.getDefaultToolkit();
65 int i = 0;
66 while ((image == null) && (i < imageExtensions.length)) {
67 byte[] imageBytes = getImageBytes(name+imageExtensions[i],
68 clazz);
69 if (imageBytes != null) {
70 image = toolkit.createImage(imageBytes);
71 }
72 i++;
73 }
74 if (image != null) {
75 imageCache.put(name, image);
76 } else {
77 image = brokenImage;
78 }
79 }
80 return image;
81 }
82
83 public static Cursor getCursor(String name, Class clazz) {
84 return getCursor(name, clazz, 0, 0);
85 }
86
87 public static Cursor getCursor(String name, Class clazz, int x, int y) {
88 Cursor cursor = (Cursor)cursorCache.get(name);
89
90 if (cursor == null) {
91 Toolkit toolkit = Toolkit.getDefaultToolkit();
92 Image image = getImage(name, clazz);
93
94 // Make this into an icon image, so that we don't have to deal
95 // with image observers.
96 ImageIcon icon = new ImageIcon(image);
97 int iconWidth = icon.getIconWidth();
98 int iconHeight = icon.getIconHeight();
99
100 // Get the best size for the cursor.
101 Dimension bestSize =
102 toolkit.getBestCursorSize(icon.getIconWidth(),
103 icon.getIconHeight());
104 int cursorWidth = bestSize.width;
105 int cursorHeight = bestSize.height;
106
107 if (cursorWidth<iconWidth || cursorHeight<iconHeight) {
108
109 // One of the dimensions is smaller, so just scale the image.
110 image = image.getScaledInstance(cursorWidth,cursorHeight,
111 Image.SCALE_DEFAULT);
112
113 } else if (iconWidth!=cursorWidth || iconHeight!=cursorHeight) {
114
115 // Both dimensions are larger. Make a larger image and draw
116 // the image into the upper, left-hand corner.
117 BufferedImage bimage =
118 new BufferedImage(cursorWidth,cursorHeight,
119 BufferedImage.TYPE_INT_ARGB);
120
121 // Draw into the image to create the new cursor.
122 Graphics2D g2d = (Graphics2D) bimage.getGraphics();
123 Color trans = new Color(0,0,0,0);
124 g2d.setColor(trans);
125 g2d.fillRect(0,0,cursorWidth,cursorHeight);
126 g2d.drawImage(icon.getImage(),0,0,null);
127
128 // Set the image to the newly created one.
129 image = bimage;
130 }
131
132 if (image == brokenImage) {
133 cursor = brokenCursor;
134 } else if (image != null) {
135 cursor = toolkit.createCustomCursor(image, new Point(x, y),
136 ""+name);
137 if (cursor != null) cursorCache.put(name, cursor);
138 }
139 }
140 return cursor;
141 }
142
143 public static Icon getIcon(String name, Class clazz) {
144 Icon icon = (Icon)iconCache.get(name);
145 if (icon == null) {
146 Image image = getImage(name, clazz);
147 if (image != null) {
148 icon = new ImageIcon(image, name);
149 }
150 if ((icon != null) && (image != brokenImage))
151 iconCache.put(name, icon);
152 }
153 return icon;
154 }
155
156
157 /**
158 * Read a string of byes from a file. */
159 private static byte[] getImageBytes(String name, Class clazz) {
160 try {
161
162 // Try first to get the data as a stream.
163 InputStream stream = clazz.getResourceAsStream(name);
164 if (stream == null) return null;
165
166 ByteArrayOutputStream ostream = new ByteArrayOutputStream();
167
168 // Create a temporary buffer.
169 int maxBytes = 10000;
170 byte[] buffer = new byte[maxBytes];
171
172 // Read the full file into the ByteArrayOutputStream.
173 int bytesRead = stream.read(buffer);
174 while (bytesRead>0) {
175 ostream.write(buffer,0,bytesRead);
176 bytesRead = stream.read(buffer);
177 }
178 stream.close();
179
180 // Convert to a byte array and return it.
181 return ostream.toByteArray();
182 } catch (IOException x) {
183
184 return null;
185 }
186 }
187 }