/* CVS: $Id: ImageCache.java,v 1.3 2001/03/08 21:08:20 gvijf Exp $ */ package gui; import java.util.*; import java.net.URL; import javax.swing.ImageIcon; /** * A simpel image cache. * The cache is of unlimited size. */ public class ImageCache { /** * Create a new image cache. * @param path The path which should be prepended to every load operation, * this path must be "" or contain a '/' at the end. */ public ImageCache(String path) { this.path = path; cache = new HashMap(); } /** * Create a new image cache. */ public ImageCache() { this(""); } /** * Get the given image. * If the image was already in the cache the cached version is returned, * otherwise the image is loaded and returned. * If the image was not found, null is returned, also in subsequent tries. */ public ImageIcon get(String imageName) { if(cache.containsKey(imageName)) return (ImageIcon) cache.get(imageName); URL url = ClassLoader.getSystemResource(path + imageName); if(url == null) { cache.put(imageName, null); return null; } ImageIcon img = new ImageIcon(url); cache.put(imageName, img); return img; } /** * The path which should be prepended to every load operation. */ private String path; /** * A hash of image names and image icons. */ private Map cache; }