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

    Question by:
    On

    Topics:

    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.

    Good Question?
    0
     

    ?

    The member who asked this question verified this comment provided the solution that solved their problem.

    Accepted Solution on 1998-09-08 at 08:18:51ID: 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.

    Top Expert Contributor

    Essential articles and videos from the Experts

    More valuable questions with Expert answers

    201511-LO-Qu-074

    Extend your technology team with the Experts Exchange community.

    — trusted by —

    Who answers my questions?Our community has technology experts around the world.

    CEHJ

    16,821

    Solutions

    Expert in:

    • Java
    • Java EE
    • Editors IDEs
    • JSP
    • Programming Languages-Other

    dpearson

    858

    Solutions

    Expert in:

    • Java
    • Java EE
    • Programming-Other
    • Game Programming
    • JSP

    Amitkumar Panchal

    460

    Solutions

    Expert in:

    • Java
    • Java EE
    • JSP
    • Oracle Database

    mccarl

    1,411

    Solutions

    Expert in:

    • Java
    • Java EE
    • Programming-Other
    • JSP
    • Algorithms

    Paul Maxwell

    9

    Articles

    2,785

    Solutions

    Expert in:

    • MS SQL Server
    • MS SQL Server 2008
    • Query Syntax
    • MS SQL Server 2005
    • Oracle Database

    ozo

    13,786

    Solutions

    Expert in:

    • Perl
    • Math / Science
    • Regular Expressions
    • Shell Scripting
    • Algorithms

    gurpsbassi

    204

    Solutions

    Expert in:

    • Java
    • Java EE
    • JSP
    • Programming-Other

    Gaurav Singh

    338

    Solutions

    Expert in:

    • Exchange
    • Windows Server 2008

    btan

    11

    Articles

    3,031

    Solutions

    Expert in:

    • Security-Other
    • Anti-Virus Apps
    • Digital Forensics
    • Encryption
    • Network Security

    RELATED TOPICS view all topics

    1. Java EE
      (16,907)
    2. Editors IDEs
      (25,095)
    3. JSP
      (15,369)
    4. Programming-Other
      (51,624)
    5. Programming Languages-Other
      (19,577)
    6. Java App Servers
      (7,000)
    7. Web Servers
      (32,320)
    8. Web Languages/Standards-Other
      (39,424)
    9. JavaScript
      (117,342)
    10. Oracle Database
      (77,149)