Jeg har haft et lignende problem og har
løst det med en lille klasse jeg skrev hvor
ud også kan hente images fra url'er.
ImageLoader.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
/**
* The ImageLoader class is used for loading images
* from various sources, including regular files,
* and files located in a jar file included in the classpath, as well as from urls.
* The class has no public constructor, it is used through
* the static methods {@link #getImage(String)} and
* {@link #getImage(InputStream)}.
* <br><b>Example of use:</b><br>
* If the image is located in a jar file it can be retrieved
* with the method invocation:
* <pre>
* Image image = ImageLoader.getImage( "myPic.gif" );
* </pre>
* If the image is located in a subdirectory images in the jar file use:
* <pre>
* Image image = ImageLoader.getImage( "images/myPic.gif" );
* </pre>
* or the previous invocation, though the latter is preferred,
* as it is more efficient if the full path is provided.
* <br>To load from a url use:
* <pre>
* Image image = ImageLoader.getImage( "
http://www.fys.dtu.dk/~janet/janetlogo.gif" );
* </pre>
* To create a label with an image use:
* <pre>
* Image image = ImageLoader.getImage( "
http://www.fys.dtu.dk/~janet/janetlogo.gif" );
* JLabel label = new JLabel( new ImageIcon( image ) );
* </pre>
* @author Carsten Knudsen
* @version 1.0
**/
public class ImageLoader {
private static String classpath = null;
private static String pathseparator = null;
private static String fileseparator = null;
private static int bufferSize = 4096;
private ImageLoader() {
} // constructor
/**
* The static method getImage tries to locate a file with the
* provided name.
* The named image may be located in a file, at a url,
* or located in a jar file, which must be in the classpath.
* If the image could not be located null is returned.
* @return the image requested or null if it could not be located
* @param name is the name of the file in which the requested image is located
**/
public static synchronized Image getImage( String name ) {
Image image = null;
URL url = null;
// attempt to load image from a file
File file = new File( name );
if ( file.exists() ) {
image = Toolkit.getDefaultToolkit().getImage( name );
if ( image != null )
return image;
} // if
// attempt to load image from a url
try {
url = new URL( name );
if ( url != null ) {
image = Toolkit.getDefaultToolkit().getImage( url );
if ( image != null )
return image;
} // if
} // try
catch ( MalformedURLException murle ) {
} // catch
// attempt to load image as a resource
url = Class.class.getResource( name );
if ( url != null ) {
image = Toolkit.getDefaultToolkit().getImage( url );
if ( image != null )
return image;
} // if
// attempt to load image from a Java archive manually
if ( classpath == null ) {
classpath = System.getProperty( "java.class.path" );
pathseparator = System.getProperty( "path.separator" );
fileseparator = System.getProperty( "file.separator" );
} // if
StringTokenizer tokenizer = new StringTokenizer( classpath, pathseparator );
while ( tokenizer.hasMoreTokens() ) {
String token = tokenizer.nextToken();
int idx = token.lastIndexOf( fileseparator );
// Extract the packagename, e.g. Janet.jar from absolute path
String packageName = token.substring( idx + 1, token.length() );
if ( packageName.endsWith( ".jar" ) ) {
try {
JarFile jar = new JarFile( token );
Enumeration enum = jar.entries();
String filename;
// iterate through all jar entries
while ( enum.hasMoreElements() ) {
JarEntry entry = (JarEntry)enum.nextElement();
filename = entry.getName();
// is it a file?
if ( ! filename.endsWith( fileseparator ) ) {
// is the current entry a candidate for the requested image
if ( filename.endsWith( name ) ) {
image = getImage( jar.getInputStream( entry ) );
if ( image != null )
return image;
} // if
} // if
} // while
} // try
catch ( IOException ioe ) {
// do not return null as there may be another file with the
// same name that may be what we are looking for
} // catch
} // if
} // while
return null;
} // getImage
public static synchronized Image getImage( InputStream is ) {
int bytes_read;
is = new BufferedInputStream( is );
byte[] buffer = new byte[ bufferSize ];
java.util.List bytes = new ArrayList();
java.util.List lengths = new ArrayList();
try {
while ( ( bytes_read = is.read( buffer ) ) != -1 ) {
bytes.add( buffer );
lengths.add( new Integer( bytes_read ) );
buffer = new byte[ bufferSize ];
} // while
is.close();
} // try
catch ( IOException ioe ) { // if anything went wrong we did our best
return null;
} // catch
int size = 0;
int[] ints = new int[ lengths.size() ];
for (int i = 0, n = lengths.size(); i < n; i++) {
size += ( ints[ i ] = ((Integer)lengths.get( i )).intValue() );
} // for
byte[] array = new byte[ size ];
int k = 0;
for (int i = 0, n = bytes.size(); i < n; i++) {
byte[] b = (byte[])bytes.get( i );
int m = ints[ i ];
for (int j = 0; j < m; j++) {
array[ k++ ] = b[ j ];
}
} // for
return new ImageIcon( array ).getImage();
} // getImage
/**
* The method setBufferSize sets the size of the buffer used
* when loading images.
* The size must be a positive quantity.
* @see #getBufferSize()
* @param size the new size of the buffer used
**/
public synchronized void setBufferSize( int size ) {
if ( size > 0 )
bufferSize = size;
} // setBufferSize
/**
* The method returns the buffer size used when loading images.
* @see #setBufferSize(int)
* @return the buffer size used when loading images
**/
public synchronized int getBufferSize() {
return bufferSize;
} // getBufferSize
public static void main( String[] args ) throws MalformedURLException {
Image image = ImageLoader.getImage( args[ 0 ] );
if ( image != null ) {
final JFrame frame = new JFrame();
frame.getContentPane().add( new JLabel( new ImageIcon( image ) ) );
frame.pack();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent we ) {
frame.dispose();
}
}
);
frame.show();
}
} // main
} // ImageLoader