KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ImageLoader


1 /*
2  * ImageLoader.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ImageLoader.java,v 1.2 2002/11/27 23:59:50 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Image JavaDoc;
25 import java.awt.MediaTracker JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 public final class ImageLoader
30 {
31     private File file;
32     private MediaTracker JavaDoc mt;
33     private Image JavaDoc image;
34
35     public ImageLoader(File file)
36     {
37         this.file = file;
38     }
39
40     public Image JavaDoc loadImage()
41     {
42         final Editor editor = Editor.currentEditor();
43         editor.setWaitCursor();
44         image = Toolkit.getDefaultToolkit().createImage(file.canonicalPath());
45         mt = new MediaTracker JavaDoc(editor);
46         try {
47             mt.addImage(image, 0);
48             mt.waitForID(0);
49         }
50         catch (Exception JavaDoc e) {
51             Log.error(e);
52         }
53         if (mt.isErrorAny())
54             image = null;
55         if (image == null) {
56             // Try again using JIMI.
57
try {
58                 Class JavaDoc c = Class.forName("com.sun.jimi.core.Jimi");
59                 Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
60                 parameterTypes[0] = Class.forName("java.lang.String");
61                 Method JavaDoc method = c.getMethod("getImage", parameterTypes);
62                 Object JavaDoc[] args = new Object JavaDoc[1];
63                 args[0] = file.canonicalPath();
64                 Object JavaDoc returned = method.invoke(null, args);
65                 if (returned instanceof Image JavaDoc)
66                     image = (Image JavaDoc) returned;
67             }
68             catch (ClassNotFoundException JavaDoc e) {
69                 // JIMI not found.
70
}
71             catch (Exception JavaDoc e) {
72                 Log.error(e);
73             }
74             mt = new MediaTracker JavaDoc(editor);
75             try {
76                 mt.addImage(image , 0);
77                 mt.waitForID(0);
78             }
79             catch (Exception JavaDoc e) {
80                 Log.error(e);
81             }
82             if (mt.isErrorAny())
83                 image = null;
84         }
85         editor.setDefaultCursor();
86         return image;
87     }
88
89     public void dispose()
90     {
91         if (image != null && mt != null) {
92             mt.removeImage(image);
93             image.flush();
94             image = null;
95             mt = null;
96         }
97     }
98 }
99
Popular Tags