1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.ui;
12
13 import java.awt.Image;
14 import java.awt.Toolkit;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URL;
18 import java.util.Hashtable;
19 import javax.imageio.ImageIO;
20 import org.apache.commons.logging.LogFactory;
21
22 /***
23 * a cached factory of images stored off this package, for things such as
24 * button icons etc
25 *
26 * @author Martin Hill
27 *
28 */
29
30
31 public class ImageFactory
32 {
33
34 private static final Hashtable imageCache = new Hashtable();
35
36 /*** Get an image suitable for message boxes, etc. Does a typecast of getIcon()
37 * for file, then for icons used by option pane in UI Manager
38 */
39 public static Image getImage(String imageName)
40 {
41
42 Image image = (Image) imageCache.get(imageName);
43 if (image != null) {
44 return image;
45 }
46
47 image = loadImage(imageName);
48
49 if (image == null) {
50 image = loadImage(imageName+".gif");
51 }
52
53 return image;
54 }
55
56 /***
57 * Loads Image from file in the images subdirectory of this class
58 */
59 public static Image loadImage(String filename)
60 {
61
62 String path = "./images/"+filename;
63 URL url = IconFactory.class.getResource(path);
64 LogFactory.getLog(IconFactory.class).debug("Looking for image "+path+" got "+url);
65 if (url != null) {
66 return Toolkit.getDefaultToolkit().getImage(url);
67 }
68
69
70
71 InputStream in = IconFactory.class.getResourceAsStream(path);
72 LogFactory.getLog(IconFactory.class).debug("getResourceAsStream returns "+in);
73 if (in != null) {
74 try {
75 return ImageIO.read(in);
76 }
77 catch (IOException ioe) {
78
79 }
80 }
81 return null;
82 }
83
84 /***
85 * Convenience routine for Loading Image from file in the images subdirectory of the given class
86 */
87 public static Image loadClassImage(Class givenClass, String filename)
88 {
89 String path = "./images/"+filename;
90
91
92 URL url = givenClass.getResource(path);
93 if (url != null) {
94 return Toolkit.getDefaultToolkit().getImage(url);
95 }
96
97 InputStream in = givenClass.getResourceAsStream(path);
98 LogFactory.getLog(IconFactory.class).debug("getResourceAsStream returns "+in);
99 if (in != null) {
100 try {
101 return ImageIO.read(in);
102 }
103 catch (IOException ioe) {
104
105 }
106 }
107 return null;
108 }
109
110
111 }
112
113
114
115
116
117
118