By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,721 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,721 IT Pros & Developers. It's quick & easy.

applet loading of images -- network or JAR approach faster?

P: 1
hi all,

i'm stumped. my applet used to load images over the network. (it was actually designed by someone else.) yes, the applet used to load each image file independently over the network and incurred a network hit per image file.

i wanted to avoid the overhead of a separate network connection for each image file, so i bundled all the images into the JAR file. yet, somehow, the loading time for the applet is slower now! i'm totally mystified.

i put the code for loading each image into a separate thread as well. here's the thread code:

Expand|Select|Wrap|Line Numbers
  1. public class ImageLoaderThread extends Thread {
  3.     private Hashtable images;
  4.     private DesignApplet applet;
  6.     public ImageLoaderThread(DesignApplet applet, Hashtable images, String imageFile) {
  7.         super(imageFile);
  8.         this.images = images;
  9.         this.applet = applet;
  10.     }
  12.     public void run() {
  13.         try {
  15.             System.out.println("Starting to load image ...");
  17.             String imageFile = getName();
  18.             InputStream is = applet.getClass().getResourceAsStream(imageFile);
  19.             int arraySize = is.available() * 2; // CH: create extra space just in case
  20.             System.out.println("Image Loader Thread: " + imageFile + "bytes available: " + arraySize);
  21.             BufferedInputStream bis = new BufferedInputStream(is);
  22.             byte[] byBuf = new byte[arraySize];  // a buffer large enough for our image
  23.             int byteRead =, 0, arraySize);
  24.             Image buttonImage = Toolkit.getDefaultToolkit().createImage(byBuf);
  25.             images.put(imageFile, buttonImage);
  26.         }
  27.         catch(Exception e) {
  28.             System.out.println("Exception while loading: " + getName());
  29.             e.printStackTrace();
  30.         }
  32.     }

imageFile is the name of an image file stored in the JAR file. it's not stored on the server anywhere, so i know the file must be created from the JAR file. despite this, the speed of image creation seems tied to the network. in other words, when i attempt to load the applet over a slow link, it takes longer to load than over a fast link.

any ideas? i'm seriously stumped and could use help. what am i doing wrong? my goals are to minimize loading time of the applet while minimizing network hits for the server.


p.s. if you want to play w/ the applet, click on the following link and click the "Customize" button:
Nov 6 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.