Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-09-03
3
Medium Priority
?
1,859 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

636 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