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
- public class ImageLoaderThread extends Thread {
- private Hashtable images;
- private DesignApplet applet;
- public ImageLoaderThread(DesignApplet applet, Hashtable images, String imageFile) {
- super(imageFile);
- this.images = images;
- this.applet = applet;
- }
- public void run() {
- try {
- System.out.println("Starting to load image ...");
- String imageFile = getName();
- InputStream is = applet.getClass().getResourceAsStream(imageFile);
- int arraySize = is.available() * 2; // CH: create extra space just in case
- System.out.println("Image Loader Thread: " + imageFile + "bytes available: " + arraySize);
- BufferedInputStream bis = new BufferedInputStream(is);
- byte[] byBuf = new byte[arraySize]; // a buffer large enough for our image
- int byteRead = bis.read(byBuf, 0, arraySize);
- Image buttonImage = Toolkit.getDefaultToolkit().createImage(byBuf);
- images.put(imageFile, buttonImage);
- }
- catch(Exception e) {
- System.out.println("Exception while loading: " + getName());
- e.printStackTrace();
- }
- }
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.
thanks!
p.s. if you want to play w/ the applet, click on the following link and click the "Customize" button: http://www.cengraving. com/s/z/Photo-plaques/PP024.jsp