Mittwoch, 31. Januar 2007

How to load an Image from a jar-file?

Problem:
"I've got my images in a jar file. Every time I try to load them, they are not loaded. How can I access the images in the jar the right way?"


Solution:

If you want to load an ImageIcon from your jar file, you can use this code:

ImageIcon image = new ImageIcon();
image = new ImageIcon("yourclass".class.getResource("/imagefolder/" + "image.gif" ));



If you want to load an Image from your jar file you can use this code:

Image image = Toolkit.getDefaultToolkit().getImage("yourclass" .class.getResource("/imagefolder/" + "image.gif"));


Be sure to replace "yourclass" with your own class and imagefolder with the imagefolder inside your jar file. And of course you need to replace image.gif with the image you want to load!

Keine Kommentare: