Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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.
0
batat
Asked:
batat
  • 2
1 Solution
 
gadioCommented:
Hi Avi. The answer divides to two. If you are using IE4 than you can jar all the classes and images together. You then open each image through url as if its not in a jar, and the IE4 caches them for you and read them from the jar. You can even use the very helpfull reasource management command Class.getReasource . In Netscape the situation is a bit difficult. The caching is not done for you if you are not using the java plug-in or the scpecial Netscape java1.1 patch. You can pack the images in a jar but read the image from the jar over the net (less efficient). If you need this sort of code I can note in a sample that does that. It will work seamlesly for Netscaep and IE4.
Don't hesitate to ask for more info.
G.
0
 
batatAuthor Commented:
Can you please give me the source code of your suggestions.

thanks,
Avi.

0
 
gadioCommented:
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.
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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now