1 /**
2  *
3  * ImageLoader Class
4  * <p>
5  * Offers a way to load an image (JPEG or GIF) from a disk location relative
6  * the a Java Class.
7  * These methods work when using the code as a StandAlone application, class files
8  * inside a Browser, and or and JAR file loaded into a Browser.
9  *
10  * To use, simply call the static method with something like this...
11  * Image myImage = ImageLoader.getImage(myComponent, "images", "filename.jpg");
12  * where myComponent is the component to display the image on.
13  *
14  * @version 1.0 May 1998
15  * @author Chris Parkinson
16  */
17 
18 import java.awt.*;
19 import java.awt.image.*;
20 import java.util.*;
21 import java.io.*;
22 import java.net.URL;
23 
24 
25 
26 public class ImageLoader
27 {
28 
29 
30 /////////////////////////////////////////////////////////////////
31 //
32 // Constructors
33 //
34 /////////////////////////////////////////////////////////////////
35 /**
36  * Construct nothing !
37  */
ImageLoader()38  public ImageLoader() {
39  }
40 
41 
42 ////////////////////////////////////////////////////////
43 //
44 // Get a Resource Image
45 //
46 ////////////////////////////////////////////////////////
47 /**
48  * Get a Resource Image.
49  * Read in an image file from a resource ( a file located in the class path)
50  * This method accesses images using the getResourceStream methods of the
51  * Class class.
52  * <p>
53  * @param owner the object who wants the image icon.
54  * @param pathname the pathname for the image .
55  * @param filename the filename for the image (GIF or JPG format).
56  * @return an Image
57  */
getImage(Component owner, String pathname, String filename)58  public static Image getImage(Component owner, String pathname, String filename)
59 {
60 
61    	byte imageBytes[] = getImageBytes(owner.getClass(), pathname, filename);
62   	if (imageBytes!=null) return getImage(owner, imageBytes);
63 	return null;
64 }
65 
66 
67 /**
68  * Get a the byte array of a Resource Image.
69  * Read in an image file from a resource ( a file located in the class path)
70  * and return the image byte array.
71  * This method accesses images using the getResourceStream methods of the
72  * Class class.	The file to access must be in the same directory as the class owner.
73  * <p>
74  * @param classOwner the class who wants the image icon.
75  * @param pathname the pathname for the image .
76  * @param filename the filename for the image (GIF or JPG format).
77  * @return an array of bytes.
78  */
getImageBytes(Class classOwner, String pathname, String filename)79 public static byte[] getImageBytes(Class classOwner,  String pathname, String
80 filename) {
81 	String fullFile = filename;
82     if (pathname!=null)fullFile  = pathname + "/" + filename;
83 
84 	//Try and load our images the correct way
85 	InputStream inputStream = classOwner.getResourceAsStream(fullFile);
86 
87 	//Now we will load whatever we have
88     try {
89 	   	byte array[] = new byte[inputStream.available()];
90 	   	inputStream.read(array);
91 	   	inputStream.close();
92 	  	return array;
93 	}catch(Exception e) {
94 		System.out.println("Error occurred trying to read a resource image ");
95 		System.out.println("Filename = " + fullFile);
96 		System.out.println("Class = " + classOwner);
97 		System.out.println(e.toString());
98 		return null;
99 	}
100 }
101 
102 /**
103  * Get an image from an array of bytes.
104  * <p>
105  * @param owner a component who owns the image
106  * @param imageData an array of bytes containing an image
107  * @return an Image
108  */
getImage( Component owner, byte[] imageData)109 public static Image getImage( Component owner, byte[] imageData) {
110   	try {
111   		Toolkit toolkit = Toolkit.getDefaultToolkit();
112 		Image image = toolkit.createImage(imageData);
113 		MediaTracker tracker = new MediaTracker(owner);
114 		tracker.addImage(image,0);
115 		tracker.waitForAll();
116 		return image;
117 	}catch(Exception e)  {}
118 	return null;
119 }
120 
121 
122 
123 
124 }
125 
126