Java tutorial
//package com.java2s; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; public class Main { private static Map UIImagesReferences = new HashMap(); public static BufferedImage getUIImage(Class c, String name) { try { String id = c + " - " + name; WeakReference wr = (WeakReference) UIImagesReferences.get(id); if (wr != null) { BufferedImage img = (BufferedImage) wr.get(); if (img != null) { return img; } } BufferedImage img = ImageIO.read(c.getResource(name)); UIImagesReferences.put(id, new WeakReference(img)); return img; } catch (Exception e) { throw new RuntimeException("error loading " + c + " " + name, e); } } /** * @see net.yura.domination.mapstore.MapChooser#createImage(java.io.InputStream) */ public static BufferedImage read(InputStream in) throws IOException { try { BufferedImage img = ImageIO.read(in); if (img == null) { throw new IOException("ImageIO.read returned null"); } return img; } finally { try { in.close(); } catch (Throwable th) { } } } }