Solved

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

Posted on 1998-09-03
3
1,840 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
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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 107
How to convert from xls to xlsx using java 7 89
restrict decimal places for double datatype 10 31
Java List 4 38
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

749 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