import browser.Applet;
import awt.Graphics;
import awt.Image;

/**
 ページの飾りの光るアプレット LightPin クラスの定義
 */
class LightPin extends Applet implements Runnable {

       static final int numberOfImages=8;  // 画像データの数

       Thread lightThread=null;            // 画像の移動を行うスレッド
       protected Image images[];           // イメージの配列
       private int count=0;                // アニメーションの何コマ目か

    /**
     初期設定の関数。
     指定された色のイメージを生成し、アプレットのサイズを設定。
     */
       protected void init() {

             String dirName="/images/lightpin/";
             String fileName=null;
             String colorName = getAttribute("color");

             if( colorName== null )
                  fileName = "red";
             else if( colorName.equals("blue") || colorName.equals("Blue") )
                  fileName = "blue";
             else if( colorName.equals("green") || colorName.equals("Green")  )
                  fileName = "green";
             else
                  fileName = "red";

             images = new Image[ numberOfImages ];

             for( int i=0; i<numberOfImages; i++ ) {
                 images[i] = getImage( dirName + fileName + (i+1) + ".gif" );
                 if( images[i] == null ) break;
             }

             resize( images[0].width, images[0].height );
       }

    /**
     アプレットの表示（初期状態）を行う関数。
     */
       public void paint( Graphics g ) {

             g.drawImage( images[0], 0, 0 );
       }

    /**
     アプレットの表示を描き直す関数。
     */
       public void update( Graphics g ) {

             g.drawImage( images[count], 0, 0 );
       }

    /**
     アプレットが画面に現れた時に呼び出される。
     */
       protected void start() {

             if( lightThread == null ) {
                   lightThread = new Thread(this);
                   lightThread.start();          //スレッド起動
             }
       }

    /**
     アプレットが画面から消えた時に呼び出される。
     */
       protected void stop() {

             if( lightThread != null ) {
                   lightThread = null;           //スレッド停止
             }
       }

    /**
     アプレットが続行する作業
     */
       public void run() {

             while( lightThread.isAlive() ) {
                  repaint();
                  count = (count+1) % numberOfImages;
                  Thread.sleep(180);            //一時停止180ミリ秒
             }
       }
}

