001 package org.util.resource; 002 003 import java.util.*; 004 import java.io.*; 005 import java.net.*; 006 import java.awt.*; 007 import java.awt.event.*; 008 import javax.swing.*; 009 import javax.swing.event.*; 010 011 import java.awt.image.*; 012 import javax.imageio.*; 013 014 import org.util.env.LaunchInformation; 015 016 public class ResourceManager { 017 018 private static BufferedImage error_image_; 019 private static ResourceManager rm_; 020 021 public static Image getImage(String path, int w, int h) { 022 URL url = findResource(path); 023 if(url!=null) { 024 Image image = null; 025 try{ 026 image = javax.imageio.ImageIO.read(url); 027 if(image.getWidth(null)!=w || image.getHeight(null)!=h) { 028 int hints = Image.SCALE_FAST; 029 image = image.getScaledInstance(w, h, hints); 030 } 031 }catch(Exception exc){} 032 if(image!=null) 033 return image; 034 } 035 return getErrorImage(w, h); 036 } 037 038 public static Image getImage(String path) { 039 URL url = findResource(path); 040 if(url!=null) { 041 Image image = null; 042 try{ 043 image = javax.imageio.ImageIO.read(url); 044 }catch(Exception exc){} 045 if(image!=null) 046 return image; 047 } 048 return getErrorImage(50, 50); 049 } 050 051 public static URL findResource(String path) { 052 053 File file = new File(path); 054 URL url = null; 055 if(file.exists()) { 056 try{ 057 url = file.toURI().toURL(); 058 }catch(Exception exc) {} 059 if(url!=null) 060 return url; 061 } 062 063 try{ 064 url = LaunchInformation.createJarURL(path); 065 }catch(Exception exc) {} 066 067 if(url==null) System.err.println("cannot find resource: "+url); 068 069 return url; 070 //return ClassLoader.getSystemResource(path); 071 } 072 073 public static Image getErrorImage(int w, int h) { 074 if(error_image_ == null) { 075 error_image_ = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 076 Graphics2D g = (Graphics2D)error_image_.getGraphics(); 077 g.setColor(Color.red); 078 g.drawRect(0,0,w-1,h-1); 079 g.drawLine(1,1,20,20); 080 g.drawLine(1,20,20,1); 081 } 082 return error_image_; 083 } 084 085 }