Solved

Loading images for an applet from a JAR file (Swing)

Posted on 1998-09-03
3
1,824 Views
Last Modified: 2013-12-22
I'd like to modify the following code so that it will load the images for the applet from the JAR file:

    public static ImageIcon loadImageIcon(Applet applet, String filename)
        throws MalformedURLException
    {
        URL url;
       
        try {
              url = new URL(applet.getCodeBase(), filename);
        } catch(MalformedURLException e) {
            throw (new MalformedURLException());
        }
       
        return new ImageIcon(url);
    }

I have tried using the constructor URL(String filename), giving a relative path to the images which did not work.

Thanks.
0
Comment
Question by:mwalker
3 Comments
 
LVL 3

Expert Comment

by:sailwind
Comment Utility
using:

new URL(applet.getCodeBase(), filename);

will not create an URL which can bypass the browser's security sandbox.

Convert it to:

url = new URL(applet.getCodeBase() + filename);

and that should work.
0
 

Author Comment

by:mwalker
Comment Utility
I'm sorry, but this didn't work either.  Here's my scenario:

In HTML file:
<APPLET NAME="MyApplet" CODEBASE="../lib" CODE="MyApplet.class" WIDTH="745" HEIGHT="475"
ALIGN="BOTTOM" CODEBASE="myapplet.jar">

All of the images are in a subdirectory of lib.  To test your proposed solution, I moved this directory after adding the images to the JAR file.
0
 
LVL 5

Accepted Solution

by:
fontaine earned 100 total points
Comment Utility
Use the following sample code as a starting point.

public static ImageIcon loadImageIcon(String filename)  {
   Image image = null;

   try {
        // get a stream to read the image
        InputStream in = getClass().getResourceAsStream(filename);
        // buffering -> more efficient
        BufferedInputStream bufIn = new BufferedInputStream(in);
        // the byte array that will contain the image
        byte bytes[] = new byte[10000];
        // read the image
        int count = bufIn.read(bytes, 0, 10000);
        // create the image from the byte array
        image = Toolkit.getDefaultToolkit().createImage(bytes, 0, count);
   } catch(Exception e) {
        // gasp, we had a problem...
   }

   if (image != null) {
      // build up the ImageIcon
      return new ImageIcon(image);
   }  

   return null;
}

Note: we do not use getResource() instead of getResourceAsStream() because some
browsers do not allow it, for security reasons. I have assumed that ImageIcon was the
ImageIcon class from Swing.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now