views:

242

answers:

3

I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:

C:\java\pacman\src

contains all of the .java class files.

C:\java\pacman\assets

contains about 4-5 images and audio files.

If I try to use the following code:

Image someFile=getCodeBase().toString() + "file.png";

The result of getCodeBase() is

file:/C:/java/pacman/bin/

However the following code fails to load:

   img=new ImgHelper(getCodeBase().toString() + "assets/");
   ImageIO.read(new File(img.getPath("pacman.png")));

Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:

file:/C:/java/pacman/bin/assets/pacman.png

saying:

Can't read input file!

But the url it gave opens fine if I paste it into run and hit enter:

So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:

public ImgHelper(String dir)
{
 //this.imgDir=dir;
 imgDir="C:\\java\\pacman\\assets\\";
}

Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?

Thanks...

+1  A: 

Why not put it all in a JAR file and then call Class.getResourceAsStream?

A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.

getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.

EDIT:

Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.

TofuBeer
A: 

I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:

 my_gif = getImage(getDocumentBase(),"imageExample.gif");
John Ellinwood
A: 

There are two possible solutions that would work:

  • The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
  • The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.

PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.

EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.

Vineet Reynolds