Solved

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

Posted on 1998-09-03
3
1,846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:sailwind
ID: 1223543
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
ID: 1223544
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
ID: 1223545
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

729 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