Ex14_1



import java.applet.*;
import java.awt.*;
import java.util.Random;

public class Ex14_1 extends Applet implements Runnable {
	int x, y;
	double theta;
	Image img[] = new Image[9];	// 画像用クラス
	Random rnd = new Random();
	Thread thd = null;
	
	public void init() {
		this.setSize(500, 300);	// アプレットの描画領域設定
		x = 50; y = 50;			// 画像初期位置
		theta = 0;
		img[0] = getImage(getCodeBase(), "img/tr1.gif");	// 画像"tr1.gif"を読込,img[0]へ
		img[1] = getImage(getCodeBase(), "img/tr2.gif");
		img[2] = getImage(getCodeBase(), "img/tr3.gif");
		img[3] = getImage(getCodeBase(), "img/tr4.gif");
		img[4] = getImage(getCodeBase(), "img/tr5.gif");
		img[5] = getImage(getCodeBase(), "img/tr9.gif");
		img[6] = getImage(getCodeBase(), "img/phone1.gif");
		img[7] = getImage(getCodeBase(), "img/sub2_ani.gif");
		thd = new Thread(this);
		thd.start();
	}
	
	public void paint(Graphics g) {
		g.drawImage(img[0], x, y, this);	// 画像 img[0] を (x,y)に表示
		g.drawImage(img[1], x+55, y, this);	// 画像 img[1] を (x+55,y)に表示
		g.drawImage(img[2], x+110, y, this);
		g.drawImage(img[3], x+165, y, this);
		g.drawImage(img[4], x+220, y, this);
		g.drawImage(img[5], x+275, y, this);
		g.drawImage(img[6], 50, 200, this);
		g.drawImage(img[7], (int)(150-3/2*x), y+100+(int)(30*Math.cos(theta)), this);
	}
	
	public void run() {
		while (true) {
			if (x < -300) {
				x = 500;
				y = rnd.nextInt(100)+50;
			} else {
				x -= 5;
			}
			theta += Math.PI/36;
			
			repaint();	// 再描画
			
			try {
				thd.sleep(50);	// スレッド一時停止 70ms
			} catch (InterruptedException e) {
			}
		}
	}
}