Here is a class that retrieves a picture file outside a jar file, from a URL, or inside a jar file.
package images;
import java.awt.Image;
import java.awt.MediaTracker;
import java.net.URL;
import javax.swing.ImageIcon;
/**
This is a utility class that allows images to be loaded from file, from a URL, or
from a JAR file.<p>
COPYRIGHT © 2007 ham90mack. All Rights Reserved.
@author ham90mack
@version 1.20 2007-06-22
*/
public class ImageRetriever
{
/**
This class is a utility and should never be constructed.
*/
private ImageRetriever(){}
/**
Retrieves an ImageIcon from file. This method first tries to get the image from
the base folder that is defines when starting the JVM. If the image is not found,
then this attempts to get the image from a folder named "images". If the image is
not found, then this attempts to load the image from a URL. If the image is not
found or if the String is not a valid URL, then this attempts to load the image from
the JAR file this may be in. Note that if an image is expected to be in a folder within
the jar file named images, "/images/" does not need to be provided. Otherwise, make sure
to put "/" at the beginning of the filename. If the image is not found, a RuntimeException
is thrown.
@param filename the image file to retrieve
@return the ImageIcon created from the file
@throws RuntimeException if the icon does not exist
@see #getImage(String)
*/
public static ImageIcon getImageIcon(String filename)
{
// to read from file
ImageIcon icon = new ImageIcon(filename);
//checks if the icon is in a folder named images
if ((icon == null) || (icon.getImageLoadStatus() != MediaTracker.COMPLETE)){
icon = new ImageIcon("images/" + filename);
}
// try to read from URL
if ((icon == null) || (icon.getImageLoadStatus() != MediaTracker.COMPLETE)) {
try {
URL url = new URL(filename);
icon = new ImageIcon(url);
} catch (Exception e) { /* not a url */ }
}
// in case file is inside a .jar
if ((icon == null) || (icon.getImageLoadStatus() != MediaTracker.COMPLETE)) {
URL url = ImageRetriever.class.getResource(filename);
if (url == null) throw new RuntimeException("image " + filename + " not found");
icon = new ImageIcon(url);
}
return icon;
}
/**
Retrieves an Image from a file. Calls getImageIcon to get the image and returns the
Image within the ImageIcon.
@param filename the image file to retrieve
@return the Image created from the file
@throws RuntimeException if the icon does not exist
@see #getImageIcon(String)
*/
public static Image getImage(String filename)
{
ImageIcon icon = getImageIcon(filename);
return icon.getImage();
}
}The bulk of what is happening is within the getImageIcon method. First it tries to get the icon from a file. If it doesn't exist, it tries to get the icon from a folder named images. If it doesn't exist, it tries to get the icon from a URL. If the String is not a valid URL or if it doesn't exist, it tries to get it from inside a JAR file using the Class getResource method. If it is still not found, a RuntimeException is thrown. If an ImageIcon is needed in a class that will be in a JAR file, this is how to use this:CODE
images.ImageRetriever.getImageIcon("blah.png");
Or import images and just use ImageRetriever.getImageIcon("blah.png");
NOTE: Please read the comment for getImageIcon for further clarification.
Edited by ham90mack, 22 June 2007 - 02:35 PM.











