Image path in Applet JAR
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Trex_Lim
Posted On:   Wednesday, June 11, 2003 04:26 AM

Anyone who may help: I am currently working on an applet that will display an icon (using a gif image file) beside a text when a user click on the list item. I tried to create a JAR file comprising all the necessary class files and the image file. When I specify the absolute filename for the image file in the constructor of ImageIcon, the icon is displayed successfully. However, when I tried to use a relative path, the image doesn't seem to be found. Assume that I have image in my C:projectimage folder while my JAR output path is C:projectapplet. With absolute path, I use the following: ImageIcon tickIcon = new ImageIcon("C:projectimage ick.gif");   More>>


Anyone who may help:



I am currently working on an applet that will display an icon (using a gif image file) beside a text when a user click on the list item.



I tried to create a JAR file comprising all the necessary class files and the image file. When I specify the absolute filename for the image file in the constructor of ImageIcon, the icon is displayed successfully. However, when I tried to use a relative path, the image doesn't seem to be found.



Assume that I have image in my C:projectimage folder while my JAR output path is C:projectapplet. With absolute path, I use the following:



ImageIcon tickIcon = new ImageIcon("C:projectimage ick.gif");



So, if I want to use relative path to refer to the file in my JAR, what should be it? Please advise. Thanks.

   <<Less

Re: Image path in Applet JAR

Posted By:   Bahman_Barzideh  
Posted On:   Wednesday, June 11, 2003 05:57 AM

I am not sure this will help you, but you can use the getResource () method of Class to locate the image by its
relative path (regardless of whether you are dealing with a JAR file or not). So let's say the relative path of the image to the program's main class (lets call it MainClass)
is known to be img/image.png. The following code
will get you the image every time:

try {
URL url = MainClass.class.getResource ("img/image.png"));
ImageIcon image = new ImageIcon (url);
}
catch (Exception ex) {
}
About | Sitemap | Contact