package main.java.gui.board;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import main.java.data.Tile;
import main.java.gui.util.Resources;

public class TileImage {

	// Private
	
	static private Hashtable<String, BufferedImage> tilesHashTable = null;
	
	static {
		tilesHashTable = new Hashtable<String, BufferedImage>();
	}
	
	// Public
	
	public static BufferedImage getImage(Tile tile) {
		String tileID = tile.getTileID();
		BufferedImage tileImage = null;
		
		tileImage = tilesHashTable.get(tileID);
		if (tileImage != null) {
			return tileImage;
		}
		
		tileImage = Resources.imageNamed(tileID);
		if (tileImage != null) { 
			tilesHashTable.put(tileID, tileImage);
			return tileImage;
		}
		
		return tileImage;
	}
	
	public static BufferedImage getRotatedImage(Tile tile) {
		BufferedImage image = TileImage.getImage(tile);
		
		AffineTransform at = new AffineTransform();
		at.translate(image.getWidth() / 2, image.getHeight() / 2);
		at.rotate(Math.toRadians(tile.getTileDirection().getVal()*90));
		at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
		
		AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
	    return op.filter(image, null);
	}
	
	public static void drawTile(Graphics2D g2d, Tile tile, int x, int y, int width) {
		BufferedImage image = TileImage.getImage(tile);
		
		AffineTransform at = new AffineTransform();
		at.translate(x + width / 2, y + width / 2);
		at.rotate(Math.toRadians(tile.getTileDirection().getVal()*90));
		at.scale((double)width / (double)image.getWidth(),
				 (double)width / (double)image.getHeight());
		at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
		g2d.drawImage(image, at, null);
		
		if (tile.isStop()) {
			int stopWidth = (int) (0.5 * width);
			g2d.setColor(Color.RED);
			g2d.fillOval(x + (width-stopWidth)/2, y + (width-stopWidth)/2, stopWidth, stopWidth);
			
			g2d.setColor(Color.WHITE);
	        FontMetrics fm = g2d.getFontMetrics();
	        Rectangle2D r = fm.getStringBounds("S", g2d);
	        int textX = (width - (int) r.getWidth()) / 2;
	        int textY = (width - (int) r.getHeight()) / 2 + fm.getAscent();
	        g2d.drawString("S", x+textX, y+textY);
		}
	}
	
}

@@ -27,60 +27,8 @@
 d;%0A%0A
-import java.awt.Color;%0Aimport java.awt.FontMetrics;%0A
 impo
@@ -93,42 +93,8 @@
 rm;%0A
-import java.awt.geom.Rectangle2D;%0A
 impo
@@ -1885,24 +1885,256 @@
  width);%0A%09%09%09
+BufferedImage bufferedImage;%0A%09%09%09bufferedImage = Resources.imageNamed(%22stop%22);%0A%09%09%09if (bufferedImage != null) %7B%0A%09%09%09%09g2d.drawImage(bufferedImage, x + (width-stopWidth)/2, y + (width-stopWidth)/2, stopWidth, stopWidth, null);%0A%09%09%09%7D%0A%09%09%09/*
 g2d.setColor
@@ -2547,16 +2547,22 @@
 +textY);
+*/%0A%09%09%09
 %0A%09%09%7D%0A%09%7D%0A

add display of stop image instead of S label