import java.applet.Applet;
import java.awt.*;

public class Test204 extends Applet implements Runnable {

	Color col[]={	Color.blue, Color.cyan, Color.green, Color.yellow,
			Color.orange, Color.pink, Color.red, Color.magenta };

	Color newColor;
	Thread test = null;

	public void init() {
		// 初期化
	}

	public void start() {
		if (test == null) {
			// スレッドを実行
			test = new Thread(this);
			test.start();
		}
	}

	public void stop() {
		if (test != null) {
			// スレッドを止める
			test.stop();
			test = null;
		}
	}

	public void run() {

		while(true) {
			// ランダムに色をセット
			int z = (int) (Math.random()*8);	// z に色No.をセット
			newColor = col[z];			// 色をセット

			// ５回明るくする
			for(int i=0; i<6; i++) {
				newColor = bright(newColor);
			}

			for (int n=0; n<10; n++) {
				repaint();
				// repaintはupdateを呼び、updateはpaintを呼ぶ

				try {
					// スレッドの一時停止時間
					Thread.sleep(500);
				} catch (InterruptedException e){ }
				newColor = dark(newColor);	// １段暗い色を作る
			}
		}
	}

	public void paint(Graphics g) {
		g.setColor(newColor);		// 色のセット
		g.fillRect(0, 0, 200, 200);	// 画面を塗る
	}

	// 明るい色を作る（コアＡＰＩ brighter()ソースの修正）
	public Color bright(Color c) {
		double b=15;
		return new Color(Math.min((int)(c.getRed()+b), 255), 
				 Math.min((int)(c.getGreen()+b), 255),
				 Math.min((int)(c.getBlue()+b), 255));
	}

	// 暗い色を作る（コアＡＰＩ darker()ソースからの修正）
	public Color dark(Color c) {
		double b=15;
		return new Color(Math.max((int)(c.getRed()-b), 0), 
				 Math.max((int)(c.getGreen()-b), 0),
				 Math.max((int)(c.getBlue()-b), 0));
	}

}
