Ex15_2



/*--------------------------
 Ex15_2.java
 対数螺旋:アニメーション版
 ---------------------------*/
package jp_bunkyo;

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

public class Ex15_2 extends Applet implements Runnable {
	double theta, a, b;		// 対数螺旋の回転角θ,定数a,b
	int cx, cy, x1, y1, x2, y2;	// 座標(x1,y1),(x2,y2),螺旋中心位置(cx,cy)
	int red, grn, blu;		// 色設定用:赤・緑・青
	int num, width, height;	// 描画回数num, 幅width, 高さheight
	Color col;
	Thread thd = null;	// スレッド用

	public void init() {	// 初期化
		this.setSize(500, 500);	// 描画領域 500×500
		cx = 200;	cy = 120;	// 螺旋の開始(中心)位置 (cx, cy) = (200, 120)
		num = 330;				// 線分・楕円の描画回数 330回
		theta = 2*Math.PI / 60;		// 対数螺旋用回転角(1回当たり)= 2π/60 = 360°/60
		a = 1.2;	 b = 0.15;		// 対数螺旋用定数
		red = 0;	grn = 125;	blu = 255;	// 赤・緑・青
		col = new Color(red, grn, blu);		// 色を混ぜて作る
		thd = new Thread(this);	// 新しいスレッド生成
		thd.start();			// スレッドの開始
	}

	public void update(Graphics g) {	// 上書きして描画するために必要
		paint(g);
	}									// 上書きしない場合はこの3行いらないのでコメントアウト

	public void paint(Graphics g) {
		g.setColor(col);
		g.drawOval(x1, y1, width, height);	// 左上(x1,y1)として幅width,高さheightの楕円を描く
	}

	public void run() {	// 絵を描くためのメソッド
		for (int i = 0; i < num; i++) {
			red = (red + 10*i) % 255;	// 赤光を10*i強く & 0〜255内にするため剰余計算
			grn = (grn + 20*i) % 255;	// 緑光を20*i強く & 0〜255内にするため剰余計算
			blu = (blu + 30*i) % 255;	// 青光を30*i強く & 0〜255内にするため剰余計算
			col = new Color(red, grn, blu);

			x1 = cx + (int)(a * Math.exp(b * theta * i) * Math.cos(theta * i));	// 1点目の座標(x1,y1)
			y1 = cy + (int)(a * Math.exp(b * theta * i) * Math.sin(theta * i));
			width = (int)(a * Math.exp(b * theta * i));	// 楕円の幅
			height= (int)(a * Math.exp(b * theta * i));	// 楕円の高さ

			repaint();	// 再描画

			try {
				thd.sleep(10);	// スレッドを10ミリ秒眠らせる(一時停止)
			} catch (InterruptedException e) {
			}
		}
	}
}