package com.nullprogram.chess.pieces;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class ImageServer {
    public static BufferedImage getTile(String name, int size) {
        BufferedImage orig = null, image = null;
        try {
            orig = ImageIO.read(ImageServer.class.getResource(name + ".png"));
            image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics2D = image.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(orig, 0, 0, size, size, null);
            graphics2D.dispose();
        } catch (java.io.IOException e) {
            // TODO: handle it
            System.out.println("Failed to fetch image: " + name);
        }
        return image;
    }
}

@@ -32,16 +32,46 @@
 ieces;%0A%0A
+import java.util.WeakHashMap;%0A
 import j
@@ -222,72 +222,952 @@
 r %7B%0A
-    public static BufferedImage getTile(String name, int size) %7B
+%0A    private static WeakHashMap%3CCacheKey, BufferedImage%3E cache%0A    = new WeakHashMap%3CCacheKey, BufferedImage%3E();%0A%0A    private static class CacheKey %7B%0A        String name;%0A        int size;%0A%0A        public CacheKey(String name, int size) %7B%0A            this.name = name;%0A            this.size = size;%0A        %7D%0A%0A        public int hashCode() %7B%0A            return name.hashCode() %5E size;%0A        %7D%0A%0A        public boolean equals(Object that) %7B%0A            if (this == that) %7B%0A                return true;%0A            %7D%0A            if (!(that instanceof CacheKey)) %7B%0A                return false;%0A            %7D%0A            CacheKey key = (CacheKey)that;%0A            return key.hashCode() == hashCode();%0A        %7D%0A    %7D%0A%0A    public static BufferedImage getTile(String name, int size) %7B%0A        CacheKey key = new CacheKey(name, size);%0A        BufferedImage cached = cache.get(key);%0A        if (cached != null) %7B%0A            return cached;%0A        %7D%0A
 %0A   
@@ -1409,25 +1409,16 @@
 hics2D g
-raphics2D
  = image
@@ -1449,25 +1449,16 @@
        g
-raphics2D
 .setRend
@@ -1501,25 +1501,16 @@
 LATION,%0A
-         
         
@@ -1591,25 +1591,16 @@
        g
-raphics2D
 .drawIma
@@ -1646,25 +1646,16 @@
        g
-raphics2D
 .dispose
@@ -1799,32 +1799,63 @@
 ame);%0A        %7D%0A
+        cache.put(key, image);%0A
         return i

Implement caching in ImageServer.