ColorTimer.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
/** 色で時間の経過を知らせる ColorTimer クラスの定義 */
public class ColorTimer extends Applet implements Runnable {
/** タイマーのスレッド */
public Thread timer = null;
/** 表示される数値 */
protected int count;
/** アプレットが画面に現れた時に呼び出される。*/
public void start() {
if( timer == null ) {
timer = new Thread(this); // スレッド生成
timer.start(); // スレッド起動
}
}
/** アプレットが画面から消えた時に呼び出される。*/
public void stop() {
if( timer != null ) {
timer.stop(); // スレッド停止
timer = null; // スレッド消去
}
}
/** スレッドとして行われる処理 */
public void run() {
try {
setBackground( Color.blue ); // 青色に
for( count=30; count>=0; count-- ){
repaint();
Thread.sleep( 1000 ); // 1秒停止
if( count==20 )
setBackground( Color.yellow ); // 黄色に
if( count==10 )
setBackground( Color.red ); // 赤色に
}
setBackground( Color.lightGray ); // 灰色に
}
catch(InterruptedException e){
System.out.println("Error");
}
}
/** 文字を描くための処理 */
public void update( Graphics g ) {
g.clearRect( 0, 0, 30, 30 );
g.drawString( String.valueOf(count), 4, 20 );
}
}