ImageIcon to Image in a jar file?

T

tiewknvc9

hi!

I am trying to get an image from an Imageicon within a jar file.

Outside of the jar file this is pretty easy,
myImage = myImageIcon.getImage();

to get from a string to an image obviously we do the following
URL url = Image.class.getResource(strFile);

that is not what I am asking though...

My function receives an ImageIcon, and I have to convert it to an Image
within the jar file...

Any ideas?
 
T

tiewknvc9

I tried this:

myImage = ImageIO.read(getClass().getResource(myImageIcon.toString()));

but it doesnt work because the toString method breaks in the jars
execution...
 
T

tiewknvc9

I also tried simply

myImage = myImageIcon.getImage();

but that causes a crash too...
 
D

danharrisandrews

Hi,

I always use the resource for a jar or for files. First make sure that
you copy your image files to your compiled sources location and then
reference your images like this. Always reference your resources that
way if you are going to eventually distribute them in a jar and it will
save to time in the end.

String location = "my/package/for/images/myimage.gif";
ImageIcon icon = null;
URL iconURL = SomeClassInJar.class.getClassLoader().getResource(
location);
if (iconURL != null) {
icon = new ImageIcon(iconURL);
}

Better yet, centralize this code in an ImageUtilities class to save
more time.

Cheers,

Dan Andrews

- - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top