Debian Codegraph @

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

raw
1 package org.freehep.util.images;
2
3 import java.awt.Cursor;
4 import java.awt.Dimension;
5 import java.awt.GraphicsEnvironment;
6 import java.awt.Image;
7 import java.awt.Point;
8 import java.awt.Toolkit;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.net.URL;
12 import java.util.Hashtable;
13
14 import javax.swing.Icon;
15 import javax.swing.ImageIcon;
16
17 /**
18 * A convenience class for handling images.
19 * Maintains a cache of images suitable for small icons which may be reused
20 * often.
21 *
22 * @author Tony Johnson
23 * @author Cal Loomis
24 * @version $Id: ImageHandler.java 8584 2006-08-10 23:06:37Z duns $
25 */
26
27 public class ImageHandler
28 {
29 public static final Image brokenImage;
30 public static final Cursor brokenCursor;
31 public static final Icon brokenIcon;
32 private static String[] imageExtensions = { ".png", ".gif", ".jpeg", ".jpg" };
33 private static Hashtable imageCache = new Hashtable();
34 private static Hashtable cursorCache = new Hashtable();
35 private static Hashtable iconCache = new Hashtable();
36
37 // only static methods
38 protected ImageHandler()
39 {
40 }
41
42 static
43 {
44 Toolkit toolkit = Toolkit.getDefaultToolkit();
45
46 brokenImage = toolkit.getImage(ImageHandler.class.getResource("BrokenImage.gif"));
47 if (brokenImage == null)
48 {
49 throw new RuntimeException("Could not load BrokenIcon .. this looks bad!");
50 }
51 brokenIcon = new ImageIcon(brokenImage,"BrokenIcon");
52
53 Image brokenCursorImage = toolkit.getImage(ImageHandler.class.getResource("BrokenCursor.gif"));
54 if (GraphicsEnvironment.isHeadless()) {
55 // if run with headless option no custom cursors are available
56 brokenCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
57 } else if (brokenCursorImage != null) {
58 brokenCursor = toolkit.createCustomCursor(brokenCursorImage, new Point(0,0), "BrokenCursor");
59 } else {
60 brokenCursor = null;
61 }
62
63 if (brokenCursor == null)
64 {
65 throw new RuntimeException("Could not load BrokenCursor .. this looks bad!");
66 }
67 }
68
69 /**
70 * Load an image from a URL. Images are cached for efficiency. If the image cannot be found
71 * will return a brokenImage. No exceptions are thrown.
72 * @param url The url of the image to load.
73 * @return The image
74 */
75 public static Image getImage(URL url)
76 {
77 Image image = null;
78 if (url!=null) {
79 image = (Image) imageCache.get(url);
80 } else {
81 image = brokenImage;
82 }
83
84 if (image==null && url!=null)
85 {
86 Toolkit toolkit = Toolkit.getDefaultToolkit();
87 // Note we dont use toolkit.createImage(url) since it returns an image
88 // even if the specified URL does not exist
89 try
90 {
91 InputStream in = url.openStream();
92 try
93 {
94 int length = in.available();
95 if (length == 0) length = 1000;
96 byte[] bytes = new byte[length];
97 for (int offset=0;;)
98 {
99 int rc = in.read(bytes,offset,length-offset);
100 if (rc<0) break;
101 if (rc>0) offset += rc;
102 else
103 {
104 length += 1000;
105 byte[] newBytes = new byte[length];
106 System.arraycopy(bytes,0,newBytes,0,offset);
107 bytes = newBytes;
108 }
109 }
110 image = toolkit.createImage(bytes);
111 }
112 finally
113 {
114 in.close();
115 }
116 }
117 catch (IOException x)
118 {
119 image = brokenImage;
120 }
121
122 // Note we SHOULD include brokenImage in the cache, otherwise repeated requests for
123 // the same non-existant url will be very slow
124 if (url!=null) imageCache.put(url, image);
125 }
126 return image;
127 }
128
129 /**
130 * Create the best cursor by reading from a URL, with hotspot (0,0).
131 */
132 public static Cursor getBestCursor(String name, Class clazz, int width, int height) {
133 return getBestCursor(name, clazz, width, height, 0, 0);
134 }
135
136 /**
137 * Create the best cursor by reading from a URL, with hotspot (x, y).
138 * The best width and height are inserted into the URL in the spots with %width
139 * and %height. Example:
140 *
141 * getBestCursor("/org/freehep/swing/images/MyIcon%wx%h.png", 32 32);
142 *
143 * @param name URL of the cursor
144 * @param clazz Class to be used for getting resource
145 * @param width suggested width
146 * @param height suggested height
147 * @param x hotspot
148 * @param y hotspot
149 * @return The cursor, the default cursor if custom cursors are not supported
150 * or brokenCursor if the cursor cannot be found
151 */
152 public static Cursor getBestCursor(String name, Class clazz, int width, int height, int x, int y) {
153 Toolkit toolkit = Toolkit.getDefaultToolkit();
154 Dimension dimension = toolkit.getBestCursorSize(width, height);
155 if ((dimension.width == 0) && (dimension.height == 0)) return Cursor.getDefaultCursor();
156 name = name.replaceAll("%w", Integer.toString(dimension.width));
157 name = name.replaceAll("%h", Integer.toString(dimension.height));
158 return getCursor(name, clazz, x, y);
159 }
160
161 /**
162 * Create a cursor by reading from a URL, with hotspot (0,0).
163 * @param url The URL to read
164 * @return The cursor, or brokenCursor if the cursor cannot be found
165 */
166 public static Cursor getCursor(URL url) {
167 return getCursor(url, 0, 0);
168 }
169
170 /**
171 * Create a cursor by reading from a URL
172 * @param url The URL to read
173 * @param x x position of hotspot
174 * @param y y position of hotspot
175 * @return The cursor, or brokenCursor if the cursor cannot be found
176 */
177 public static Cursor getCursor(URL url, int x, int y)
178 {
179 Cursor cursor = (url!=null) ? (Cursor) cursorCache.get(url) : null;
180 if (cursor == null)
181 {
182 Toolkit toolkit = Toolkit.getDefaultToolkit();
183 Image image = getImage(url);
184 if (image == brokenImage) cursor = brokenCursor;
185 else cursor = toolkit.createCustomCursor(image, new Point(x, y), url.getFile());
186 if (url != null) cursorCache.put(url, cursor);
187 }
188 return cursor;
189 }
190
191 /**
192 * Create an Icon by reading from a URL
193 * @param url The URL to read
194 * @return The icon, or brokenIcon if it cannot be loaded
195 */
196 public static Icon getIcon(URL url)
197 {
198 Icon icon = (url!=null) ? (Icon) iconCache.get(url) : null;
199 if (icon == null)
200 {
201 Image image = getImage(url);
202 if (image == brokenImage) icon = brokenIcon;
203 else icon = new ImageIcon(image,url.getFile());
204 if (url != null) iconCache.put(url, icon);
205 }
206 return icon;
207 }
208 /**
209 * Convenience routine, equivalent to getImage(clazz.getResource(name));
210 * As an additional convenience, if the name does not contain an extension (eg .gif)
211 * then a list of common extensions will be tried.
212 * @param name The relative address of the image to load
213 * @param clazz The class from which the address is based
214 * @return The image, or brokenImage if no image is found.
215 */
216 public static Image getImage(String name, Class clazz)
217 {
218 if (name.indexOf('.') >= 0) return getImage(clazz.getResource(name));
219 for (int i=0; i<imageExtensions.length; i++)
220 {
221 URL url = clazz.getResource(name+imageExtensions[i]);
222 if (url != null) return getImage(url);
223 }
224 return brokenImage;
225 }
226
227 /**
228 * Convenience routine, equivalent to getCursor(clazz.getResource(name));
229 * As an additional convenience, if the name does not contain an extension (eg .gif)
230 * then a list of common extensions will be tried.
231 * @param name The relative address of the cursor to load
232 * @param clazz The class from which the address is based
233 * @return The cursor, or brokenCursor if no cursor is found.
234 */
235 public static Cursor getCursor(String name, Class clazz) {
236 return getCursor(name, clazz, 0, 0);
237 }
238
239 /**
240 * Convenience routine, equivalent to getCursor(clazz.getResource(name));
241 * As an additional convenience, if the name does not contain an extension (eg .gif)
242 * then a list of common extensions will be tried.
243 * @param name The relative address of the cursor to load
244 * @param clazz The class from which the address is based
245 * @param x hotspot
246 * @param y hotspot
247 * @return The cursor, or brokenCursor if no cursor is found.
248 */
249 public static Cursor getCursor(String name, Class clazz, int x, int y)
250 {
251 if (name.indexOf('.') >= 0) return getCursor(clazz.getResource(name), x, y);
252
253 for (int i=0; i<imageExtensions.length; i++)
254 {
255 URL url = clazz.getResource(name+imageExtensions[i]);
256 if (url != null) return getCursor(url, x, y);
257 }
258 return brokenCursor;
259 }
260
261 /**
262 * Convenience routine, equivalent to getIcon(clazz.getResource(name));
263 * As an additional convenience, if the name does not contain an extension (eg .gif)
264 * then a list of common extensions will be tried.
265 * @param name The relative address of the icon to load
266 * @param clazz The class from which the address is based
267 * @return The icon, or brokenIcon if no icon is found.
268 */
269 public static Icon getIcon(String name, Class clazz)
270 {
271 if (name.indexOf('.') >= 0) return getIcon(clazz.getResource(name));
272
273 for (int i=0; i<imageExtensions.length; i++)
274 {
275 URL url = clazz.getResource(name+imageExtensions[i]);
276 if (url != null) return getIcon(url);
277 }
278 return brokenIcon;
279 }
280 }