| Author |
Load images from JAR file
|
Henrik Engert
Ranch Hand
Joined: Apr 26, 2005
Posts: 70
|
|
Hi, I was wondering if there is an easy way to load images from a JAR file? For example if the images are located in the following package: com/foo/images Inside images there are say 20 images, but there could be more. How can I loop through these images in an easy way?
|
SCJP 5.0, SCWCD
|
 |
colin shuker
Ranch Hand
Joined: Apr 11, 2005
Posts: 712
|
|
Hi, Apparently its possible to load images that are contained within a JAR, but I could never get it to work, so I came up with my own solution: Basically load the pic in the normal way, then scan it to get the pixel data. You then save this, so you can generate BufferedImages at runtime from the pixel data. Drawback is, this is probably only a good idea for small pics, such as chess pieces (50 by 50 pngs in my case). Another possibility is to save a text file in the jar with the same pixel data, and then do the same thing. But I'm sure the people here can show you the proper way to do it with images in jars.
|
 |
Henrik Engert
Ranch Hand
Joined: Apr 26, 2005
Posts: 70
|
|
I am able to load one image at a time IF I know the image name, which comes from a database. But I would like to load them all once and for all. I would like to load the images just like you list what files are in a directory, but these are in a JAR file.... Anybody else have an idea?
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
Take a look at the class JarFile in java.util.jar
|
 |
Harjit Singh
Ranch Hand
Joined: Feb 01, 2007
Posts: 77
|
|
As keith mentioned take a look at the util.jar specifically the JarFile Class and use the getEnteries() to get an enumeration of all the files in the jar. You can then iterate thru the enumeration to see if any of them are images and then load them. Hope this helps - Harjit
|
 |
 |
|
|
subject: Load images from JAR file
|
|
|