1 21 22 package org.armedbear.j; 23 24 import java.awt.Image ; 25 import java.awt.MediaTracker ; 26 import java.awt.Toolkit ; 27 import java.lang.reflect.Method ; 28 29 public final class ImageLoader 30 { 31 private File file; 32 private MediaTracker mt; 33 private Image image; 34 35 public ImageLoader(File file) 36 { 37 this.file = file; 38 } 39 40 public Image loadImage() 41 { 42 final Editor editor = Editor.currentEditor(); 43 editor.setWaitCursor(); 44 image = Toolkit.getDefaultToolkit().createImage(file.canonicalPath()); 45 mt = new MediaTracker (editor); 46 try { 47 mt.addImage(image, 0); 48 mt.waitForID(0); 49 } 50 catch (Exception e) { 51 Log.error(e); 52 } 53 if (mt.isErrorAny()) 54 image = null; 55 if (image == null) { 56 try { 58 Class c = Class.forName("com.sun.jimi.core.Jimi"); 59 Class [] parameterTypes = new Class [1]; 60 parameterTypes[0] = Class.forName("java.lang.String"); 61 Method method = c.getMethod("getImage", parameterTypes); 62 Object [] args = new Object [1]; 63 args[0] = file.canonicalPath(); 64 Object returned = method.invoke(null, args); 65 if (returned instanceof Image ) 66 image = (Image ) returned; 67 } 68 catch (ClassNotFoundException e) { 69 } 71 catch (Exception e) { 72 Log.error(e); 73 } 74 mt = new MediaTracker (editor); 75 try { 76 mt.addImage(image , 0); 77 mt.waitForID(0); 78 } 79 catch (Exception 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 |