Ex14_1



/*-----------------
 Ex14_1.java
 絵の読み込みと動作
 ------------------*/
import java.applet.*;
import java.awt.*;

public class Ex14_1 extends Applet implements Runnable {
	int x, y, x2, y2;	// 位置座標用 (x, y) (x2, y2)
	double theta;		// 角度(θラジアン)
	Image img[] = new Image[3];	// 画像用クラス
	Thread thd = null;			// スレッド

	public void init() {	// 初期設定
		this.setSize(500, 300);
		x = 50;	y = 50;
		x2 = 300; y2 = 200;
		theta = 0;
		img[0] = getImage(getCodeBase(), "img/tr1.gif");	// 配列 img[0] に画像ファイル読込
		img[1] = getImage(getCodeBase(), "img/tr2.gif");
		img[2] = getImage(getCodeBase(), "img/teku2.gif");
		thd = new Thread(this);
		thd.start();				// スレッド開始
	}

	public void paint(Graphics g) {
		g.drawImage(img[0], x,    y, this);	// 配列 img[0] の画像ファイルを (50, 50) に表示
		g.drawImage(img[1], x+55, y, this);
		g.drawImage(img[2], x2, (int)(y2+30*Math.cos(theta)), this);
	}

	public void run() {
		while (true) {		// 無限ループ
			if (x < -150) x = 500; else x -= 5;
			if (x2 < -30) x2 = 500; else x2 -= 1;
			theta += Math.PI/36;	// 角度変更

			repaint();	// 再描画

			try {
				thd.sleep(70);
			} catch (InterruptedException e) {
			}
		}

	}
}