/* 2008/11/30, 2009/01/23

javac -encoding UTF8 TestRotate2.java
java TestRotate2
sample1.gif を用意

・[[TestRotate1.java]]
・[[TestRotate3.java]]
・[[TestKeyRepeat1.java]]

・TestRotate1の続き
  矢印キーに応答して、背景画像を動かしてみる。
  まずは平行移動で上下左右に動かす。
 */

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JFrame;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Operators;

public class pGraphics extends JPanel implements Runnable {

	int frameInterval = 10;
	boolean init = true;
	ArrayList<String> resultOps = new ArrayList<String>();

	Blocks aBlock = new Blocks("A",100, 100);
	Blocks bBlock = new Blocks("B",100, 50);
	// ポインタわたし
	Blocks movingBlock = aBlock;

	public pGraphics() {
		resultOps.add("remove A from on top B");
		resultOps.add("put A down on the table");
		resultOps.add("pick up B from the table");
		resultOps.add("Place B on A");

		try {
			this.init();
			JFrame f = new JFrame("Plannner");
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.getContentPane().add(this);
			f.pack();
			f.setSize(new Dimension(800, 600));
			f.setResizable(false); // リサイズ禁止
			f.setVisible(true);
			// this.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}

	public void init() {
	}

	/*
	 * public Graphics2D createGraphics2D(int width, int height, BufferedImage
	 * bi, Graphics g) { Graphics2D g2 = bi.createGraphics();
	 * g2.setBackground(Color.WHITE); g2.clearRect(0, 0, width, height); return
	 * g2; }
	 */
	// (note)呼ばれていないみたい
	public void update(Graphics g) {
		addLog("update");
		paint(g);
	}

	int c = 0;

	public void paint(Graphics g) {
		Dimension d = getSize();
		int w = (int) d.getWidth();
		int h = (int) d.getHeight();
		int gr = h - 100;
		super.paint(g); // JPanelのクリア

		/*
		 * 現状態の描写
		 */
		g.setColor(Color.BLUE);
		g.fill3DRect(aBlock.x, gr - aBlock.y, 50, 50, true);
		g.setColor(Color.YELLOW);
		g.fill3DRect(bBlock.x, gr - bBlock.y, 50, 50, true);

		paintGround(g, w, h);
	}

	public void paintGround(Graphics g, int w, int h) {
		g.setColor(Color.PINK);
		g.fill3DRect(0, h - 100, w, h, false);
	}

	/**
	 * 次の状態に遷移する 今のオペレーターの処理が終了したらTRUEを返す
	 * 
	 * @param op　OPをストリングで
	 * @return 今のオペレーターが終了したらTRUE
	 */
	boolean nextState(String op) {
		if (op.startsWith("remove")) {
			
			// AからBを持ち上げる。AのY座標を+1していきBのY座標との差が100になったら終了
			aBlock.y++;
			if (aBlock.y - bBlock.y >= 150)
				return true;
			return false;
		
		} else if (op.startsWith("put")) {
			// Aをテーブルの上におく
			// 要：テーブルがあいてるかどうかの判定
			if (aBlock.x >= 200) {
				// Y方向移動
				aBlock.y--;
				if (aBlock.y <= 50)
					return true;
				return false;
			}
			// X方向移動
			aBlock.x++;
			return false;
		} else if (op.startsWith("pick")) {
			// Bをテーブルから持ち上げる
			bBlock.y++;
			if (bBlock.y >= 150)
				return true;
			return false;
		} else if (op.startsWith("Place")) {
			// BをAの上におく
			if (bBlock.x >= 200) {
				// Y方向移動
				bBlock.y--;
				if (bBlock.y <= 100)
					return true;
				return false;
			}
			// X方向移動
			bBlock.x++;
			return false;
		}
		return true;
	}

	/*
	 * Runnable
	 */

	Thread thread;

	public void start() {
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public synchronized void stop() {
		thread = null;
	}

	public void run() {
		/*
		 * remove put pick up Place
		 */
		while (!isShowing() || getSize().width == 0) {
			try {
				thread.sleep(200);
			} catch (InterruptedException e) {
			}
		}
		// オペレーターのインデックス
		int i = 0;
		while (thread != null) {
			// オペレーターの処理をする
			if( i< resultOps.size())
				if (nextState(resultOps.get(i)))
					i++;
				
			repaint();
			try {
				thread.sleep(frameInterval);
			} catch (InterruptedException e) {
			}

			// 処理時間を眺める
			long t = System.nanoTime();
			//addLog("step:" + ((t - debugT1) / 1000000) + "msec");
			debugT1 = t;
		}
		thread = null;
	}

	long debugT1; // 更新間隔を眺める

	protected void addLog(String s) {
		System.out.println(s);
	}
}

class Blocks {
	static ArrayList<Blocks> allBlocks=new ArrayList<Blocks>();
	String name;
	int x;
	int y;

	Blocks(String name,int x, int y) {
		this.name=name;
		this.x = x;
		this.y = y;
		Blocks.allBlocks.add(this);
	}

}
