Jason Rogers
Jason Rogers

Reputation: 19344

JApplets: Loading Images

After having spend 1 year working on Android I'm a bit rusty in Traditional Java GUI.

I need to know 2 things in the way I'm opening Images

but first some code

/**
     * Load the image for the specified frame of animation. Since
     * this runs as an applet, we use getResourceAsStream for 
     * efficiency and so it'll work in older versions of Java Plug-in.
     */
    protected ImageIcon loadImage(String imageNum, String extension) {
        String path = dir + "/" + imageNum+"."+extension;
        int MAX_IMAGE_SIZE = 2400000;  //Change this to the size of
        //your biggest image, in bytes.
        int count = 0;
        BufferedInputStream imgStream = new BufferedInputStream(
                this.getClass().getResourceAsStream(path));
        if (imgStream != null) {
            byte buf[] = new byte[MAX_IMAGE_SIZE];
            try {
                count = imgStream.read(buf);
                imgStream.close();
            } catch (java.io.IOException ioe) {
                System.err.println("Couldn't read stream from file: " + path);
                return null;
            }
            if (count <= 0) {
                System.err.println("Empty file: " + path);
                return null;
            }
            return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

And I call it like this

loadImage("share_back_img_1_512", "jpg");

My problem is : How to make it more dynamic.

For the moment I'm testing on a few images but I have something like a 100 images for the final applet.

I had to store the images in a package to be able to access them.

so here is the question:

Is there a way to Load images depending on the content of a package? getting the name, size, extension...?

Basically a simpler way to generate the ImageIcons

Upvotes: 0

Views: 128

Answers (1)

Mike Clark
Mike Clark

Reputation: 10136

The way you've written your stream read -- it could result in a partial read, since just one call to read is not guaranteed to return all the bytes that the stream may eventually produce.

Try Apache commons IOUtils#toByteArray(InputStream), or include your own simple utility method:

public static final byte[] readBytes(final InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(Short.MAX_VALUE);
    byte[] b = new byte[Short.MAX_VALUE];
    int len = 0;
    while ((len = is.read(b)) != -1) {
        baos.write(b, 0, len);
    }
    return baos.toByteArray();
}

As for your organizational concerns... there is no simple+reliable way to to get a "directory listing" of the contents of a package. Packages may be defined across multiple classpath entries, spanning JARs and folders and even network resources.

If the packages in question are contained within a single JAR, you could consider consider something like what is described here: http://www.rgagnon.com/javadetails/java-0513.html

A more reliable and portable way might be to maintain a text file that contains a list of the images to load. Load the list as a resource, then use the list to loop and load all images listed in the text file.

Upvotes: 1

Related Questions