Ex14_3



/*----------
 Ex14_3.java
 絵のアニメ
 -----------*/
package jp_bunkyo;

import java.applet.*;
import java.awt.*;

public class Ex14_3 extends Applet implements Runnable {
	int x,y, x2,y2, x3,y3;	// 位置座標 (x,y), (x2,y2), (x3,y3)
	double theta;
	Image img, img2, img3;	// 3つの画像用
	Thread thd = null;	// スレッド

	public void init() {		// 初期化
		this.setSize(400, 200);	// 画面サイズ設定
		x = 300;	y = 60;		// 絵1の初期位置 (x, y)設定
		x2 = 0;	y2 = 170;	// 絵2の初期位置 (x2,y2)設定
		x3 = 0;	y3 = 130;	// 絵3の初期位置 (x3,y3)設定
		theta = 0;
		img = getImage(getCodeBase(), "img/hikosen7.gif");
		img2 = getImage(getCodeBase(), "img/sub3_ani.gif");
		img3 = getImage(getCodeBase(), "img/drive2.gif");

		thd = new Thread(this);	// スレッド
		thd.start();			// スレッド開始
	}

	public void paint(Graphics g) {
		g.drawImage(img, x, (int)(y+50*Math.cos(theta)), this);
		g.drawImage(img2, x2, y2, this);
		g.drawImage(img3, x3, y3, this);
	}


	public void run() {
		while (true) {
			while (x > -50) {
				x -= 5;
				x2 = (x2 + 7) % 400;
				if (x3 < 420) x3 +=10; else x3 = -20;
				if (y3 > -20) y3 -= 2; else y3 = 210;
				theta += Math.PI/24;
				repaint();

				try {
					thd.sleep(70);	// スレッド・スリープ(500ミリ秒)
				} catch (InterruptedException e) {
				}
			}
			x = 400;
		}
	}
}