slow loading images

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;
}
 
A

ak

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
how big are your images?

____________

http://reader.imagero.com the best java image reader.
 
A

ak

ok, tested it now, here my results:

i have 2 images com/imagero/test.png image (in jar) and test.png NOT in jar
test class:

public class JarTest {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();

String s = "com/imagero/test.png";
// String s = "test.png";

URL url = JarTest.class.getResource(s);
System.out.println(url);

InputStream in = JarTest.class.getClassLoader().getResourceAsStream(s);
System.out.println(in);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[32 * 1024];
while(true) {
int r = in.read(buffer);
if(r > 0) {
bout.write(buffer, 0, r);
}
else {
break;
}
}
Image img = Toolkit.getDefaultToolkit().createImage(bout.toByteArray());
ImageIcon icon = new ImageIcon(img);
long end = System.currentTimeMillis();
System.out.println(end - start);

JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(icon));
frame.pack();
frame.show();
}
}

here is output for String s = "com/imagero/test.png"; // (in jar):

null
java.util.zip.ZipFile$ZipFileInputStream@1fbe93
922

here is output for String s = "test.png"; //file system:
null
java.io.BufferedInputStream@2e000d
921

as you can see, there is no _any_ difference in load time.

note that JarTest.class.getResource(s); returns always null (don't ask me
why).


____________

http://reader.imagero.com the best java image reader.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top