views:

115

answers:

2

I have a java applet where I've changed the image icon that appears in the top left corner of the window. The code I use is this:

    Toolkit kit = Toolkit.getDefaultToolkit();
    Image frameIcon = kit.getImage("src/myapp/resources/logo.png");        
    getFrame().setIconImage(frameIcon);

Everything works fine until I deploy the applet to a standalone jar. In this case the icon that shows is the default icon, as if the code couldn't find the image. But the image is inside, although it is in the folder: myapp/resources/

What am I doing wrong here? Is this some weird java bug?

A: 

Are you sure you export your source code within the jar file? Because since your image is in "src/myapp/resources/logo.png", you must include your "src/myapp/resources" folder within your jar file.

But I'd recommend you to put your images in another folder, like "resources", at the root of your application folder (i.e. at the root of your jar file), and then you would be able to export an applet without the source code.

Goulutor
A: 

I have managed to find a workaround for this. I changed the:

Image frameIcon = kit.getImage("src/myapp/resources/logo.png");     

to

Image frameIcon = kit.getImage("logo.png");     

and then deploy the jar. After, I copy the image to the same place where the .class file is inside the jar and it loads ok. I don't like this workaround but it will have to do for now. The src/resources folder exists and has the image inside but it doesn't load. I think this a path specification problem but I haven't found the solution for this yet...

If the "src/myapp/resources" folder actually exists in your deployed applet jar (why not just "resources"?) you can just prepend a leading "/" onto the directory - that will make it start at the base of the file system inside the JAR. `kit.getImage("/src/myapp/resources/logo.png");` should work.
Nate