Link to home
Start Free TrialLog in
Avatar of batat
batat

asked on

images in jar

Hi,

I am using applet which loads lots of small gifs.
I tried to archive the gifs with the class files, but when
 my applet tried loading them from the jar file , i got an exception.

Is there a way to jar also the images my applet uses ?

thanks,
Avi Kavas.
ASKER CERTIFIED SOLUTION
Avatar of gadio
gadio

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of batat
batat

ASKER

Can you please give me the source code of your suggestions.

thanks,
Avi.

Shalom Avi.

Heres the code you can use:

import java.applet.Applet;
import java.awt.*;
import java.net.URL;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import rsctst.*;

public class resource_test extends Applet {

  /**
   * Initializes the applet.  You never need to call this directly; it is
   * called automatically by the system once the applet is created.
   */
  public void init() {
        setLayout( new FlowLayout());
        add(new TextField("images/image.gif"));
        rs r = new rs();
        InputStream rs_stream = null;
        rs_stream = getResourceInputStream( r.getClass(), "images/image1.gif" );
        System.out.println( rs_stream );
        System.out.println( getParameter("archive") );
        System.out.println( getParameterInfo() );
  }

  /**
   * Called to start the applet.  You never need to call this directly; it
   * is called when the applet's document is visited.
   */
  public void start() {
  }

    public InputStream getResourceInputStream( Class c, String resource ) {
        String archive;
        InputStream result_stream = null;
        String name = c.getName();
        String path = new String();
        String item = new String();
        StringTokenizer st = new StringTokenizer( name, "." );
        URL resulturl = null;
        while( st.hasMoreElements() ) {
            path+=item;
            item = (String)st.nextElement()+"/";
        }
        if( (archive = getParameter("archive")) == null ) {
            try {
              resulturl = new URL(getCodeBase(),path+resource);
              result_stream = resulturl.openStream();
            } catch( Exception e ) {
                System.out.println("E:"+e);
            }
        }
        else {
            try {
              resulturl = new URL(getCodeBase(),archive);
              InputStream in = resulturl.openStream();
              ZipInputStream zin = new ZipInputStream(in);
              ZipEntry ze;
              while( !((ze = zin.getNextEntry()).getName()).equals(path+resource));
              result_stream = resulturl.openStream();
            } catch( Exception e ) {
                System.out.println("Ex:"+e);
            }
        }
        return result_stream;
    }
}




Note that what you need is the function getReasourceInputStream, through which you read the image. The reasource will always be looked in relation to the location of class c that you send. That enables you to put all the imges in a centeral archive or with the relevent classes.
Good luck,
Gadi.