I
Ike
I have the following method, which allows me to load images, given a string
to the image, and loads the image whether it resides on the local machine,
or in a jar on the local machine. It then caches the image so that
subsequent calls for it (by the string passed in as the key to it) do not
need to be recreated.
I have a jar, with about 500 images in it. Seemingly takes a year to load
all of these. Is there a faster way to take 500 png files from a jar and
create 500 ImageIcons from the images? Thanks, Ike
public Image toImageFromFile(Applet applet,String f, boolean
useMediaTracker){
if(images==null)
images=new Hashtable();
else{
if (images.containsKey(f)) {
return (Image)images.get(f);
}
}
Toolkit tk = Toolkit.getDefaultToolkit();
Image image;
URL url = getClass().getResource(f);
if(url!=null)
image = tk.getImage(url);
else //local, not in jar
image = tk.getImage(f);
if(useMediaTracker){
java.awt.MediaTracker tracker = new
java.awt.MediaTracker(applet);
tracker.addImage(image, 0);
try { tracker.waitForID(0); }
catch (InterruptedException exception) {}
catch(Exception e){
System.out.println("Can't load "+f);
}
}
images.put(f,image);
return image;
}
to the image, and loads the image whether it resides on the local machine,
or in a jar on the local machine. It then caches the image so that
subsequent calls for it (by the string passed in as the key to it) do not
need to be recreated.
I have a jar, with about 500 images in it. Seemingly takes a year to load
all of these. Is there a faster way to take 500 png files from a jar and
create 500 ImageIcons from the images? Thanks, Ike
public Image toImageFromFile(Applet applet,String f, boolean
useMediaTracker){
if(images==null)
images=new Hashtable();
else{
if (images.containsKey(f)) {
return (Image)images.get(f);
}
}
Toolkit tk = Toolkit.getDefaultToolkit();
Image image;
URL url = getClass().getResource(f);
if(url!=null)
image = tk.getImage(url);
else //local, not in jar
image = tk.getImage(f);
if(useMediaTracker){
java.awt.MediaTracker tracker = new
java.awt.MediaTracker(applet);
tracker.addImage(image, 0);
try { tracker.waitForID(0); }
catch (InterruptedException exception) {}
catch(Exception e){
System.out.println("Can't load "+f);
}
}
images.put(f,image);
return image;
}