import java.applet.Applet;
import java.awt.*;

public class Othello02 extends Applet implements Runnable
{
	static final int BOXWID = 64;
	static final int NONE = 0;
	static final int BLACK = 1;
	static final int WHITE = 2;
	static final int OUT = 3;
	MediaTracker mt = new MediaTracker(this);
	int[][] box = new int[10][10];
	Image[] black = new Image[9];
	Image[] white = new Image[9];
	Image offImage;
	Graphics offScreen;
	Graphics screen;
	Thread thread;
	int cursorX, cursorY;

	public void init() {
		Dimension d = size();
		screen = getGraphics();
		offImage = createImage(d.width, d.height);
		offScreen = offImage.getGraphics();
		box[4][4] = BLACK;
		box[4][5] = WHITE;
		box[5][4] = WHITE;
		box[5][5] = BLACK;
		for (int n = 1; n <= 8; n++) {
			black[n] = getImage(getCodeBase(), "../../image/25_0"+n+".GIF");
			mt.addImage(black[n], 0);
			white[n] = getImage(getCodeBase(), "../../image/26_0"+n+".GIF");
			mt.addImage(white[n], 0);
		}
		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
		}
	}

	public void paint(Graphics g) {
		g.drawImage(offImage, 0, 0, null);
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void start() {
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public void stop() {
		if (thread != null) {
			thread.stop();
		}
		thread = null;
	}

	public void run() {
		int count = 1;
		Dimension d = size();
		offScreen.setColor(Color.black);
		offScreen.fillRect(0, 0, d.width, d.height);
		repaint();
		while (true) {
			int minX = 8;
			int minY = 8;
			int maxX = 1;
			int maxY = 1;
			for (int x = 1; x <=8; x++) {
				for (int y = 1; y <= 8; y++) {
					boolean rotate = false;
					if (box[x][y] == BLACK) {
						offScreen.drawImage(black[count], (x-1)*BOXWID, (y-1)*BOXWID, null);
						rotate = true;
					} else if (box[x][y] == WHITE) {
						offScreen.drawImage(white[count], (x-1)*BOXWID, (y-1)*BOXWID, null);
						rotate = true;
					}
					if (rotate) {
						if (x < minX) minX = x;
						if (y < minY) minY = y;
						if (x > maxX) maxX = x;
						if (y > maxY) maxY = y;
					}
				}
			}
			if (++count > 8) count = 1;
			repaint((minX-1)*BOXWID, (minY-1)*BOXWID, (maxX-minX+1)*BOXWID, (maxY-minY+1)*BOXWID);
			try {
				thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
	}

	public boolean mouseMove(Event evt, int x, int y) {
		int wx = x/BOXWID+1;
		int wy = y/BOXWID+1;
		if (wx != cursorX || wy != cursorY) {
			if (box[cursorX][cursorY] == NONE) {
				offScreen.setColor(Color.black);
				offScreen.fillRect((cursorX-1)*BOXWID, (cursorY-1)*BOXWID, BOXWID, BOXWID);
			}
			if (box[wx][wy] == NONE) {
				offScreen.setColor(Color.darkGray);
				offScreen.fill3DRect((wx-1)*BOXWID, (wy-1)*BOXWID, BOXWID, BOXWID, true);
			}
			cursorX = wx;
			cursorY = wy;
			repaint();
		}
		return true;
	}

	public boolean mouseExit(Event evt, int x, int y) {
		if (box[cursorX][cursorY] == NONE) {
			offScreen.setColor(Color.black);
			offScreen.fillRect((cursorX-1)*BOXWID, (cursorY-1)*BOXWID, BOXWID, BOXWID);
		}
		cursorX = cursorY = 0;
		repaint();
		return true;
	}
}

