FlipFlop.java



import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Event;

/** 複数の画像の表示を切り替える FlipFlop のクラス */

public class FlipFlop extends Applet implements Runnable {

     /** 画像の数の最大値 */
       protected static final int limitNumber=16;

     /** 画像を囲む枠の幅 */
       protected static final int border=4;

     /** 画像の切り替えを行うスレッド */
       public Thread flipThread=null;

     /** イメージ・データ */
       public Image images[];

     /** イメージのファイル名 */
       public String fileNames[];

     /** イメージの個数、サイズの限界 */ 
       protected int  maxNumber=0, imageWidth=0, imageHeight=0;

     /** イメージ切り替えの間隔(ミリ秒単位で指定)*/ 
       public int intervalTime=1000;

     /** 回転の表示の状態( 0->1->2->3->0->1.... ) */ 
       protected int showMode = 0;

     /** 表示されるイメージの番号 */ 
       protected int counter=0;

     /** 初期設定のメソッド */
       public void init() {

          // ファイル名をパラメータから得る、個数の確認

             fileNames = new String[limitNumber];
             for( int i=0; i<limitNumber ; i++ ) {
                  fileNames[i] = getParameter( "file" + (i+1) );
                  if( fileNames[i] == null ) {
                      maxNumber = i; break;
                  }
             }

          // イメージオブジェクトの生成

             images = new Image[maxNumber];
             for( int i=0; i<maxNumber; i++ ) {
                 images[i] = getImage( getDocumentBase(), fileNames[i] );
             }

          // 描画範囲のサイズの設定

             imageWidth = size().width - border*2;
             imageHeight = size().height - border*2;
       }

     /** アプレットの表示を行うメソッド */
       public void paint( Graphics g ) {

             g.setColor( Color.black );
             g.fill3DRect( 0, 0, 
                   imageWidth+border*2, imageHeight+border*2, false );
             g.setColor( Color.yellow );
             g.fill3DRect( (imageWidth+border-4)/2, border-2,
                            4, imageHeight+4, false );
             if( showMode == 1 ) {
                   g.setColor( Color.lightGray );
                   g.fill3DRect( (border+imageWidth/2-6)/2, border,
                                  imageWidth/2+7, imageHeight, true );
             }
             else if( showMode == 2 ) {
                   g.setColor( Color.lightGray );
                   g.fill3DRect( (border+imageWidth-6)/2, border,
                                  7, imageHeight, true );
             }
             else if( showMode == 3 ) {
                   g.setColor( Color.gray );
                   g.fill3DRect( (border+imageWidth/2-6)/2, border,
                                  imageWidth/2+7, imageHeight, true );
             }
             else {
                 if( 0 <= counter && counter < maxNumber ) {
                      g.clearRect( border, border, imageWidth, imageHeight );
                      g.drawImage( images[counter], border, border,
                                   imageWidth, imageHeight, this );
                 }
             }
       }

     /** アプレットが画面に現れた時に呼び出される。*/
       public void start() {
             if( flipThread == null ) {
                   flipThread = new Thread(this);
                   flipThread.start();
             }
       }

     /** アプレットが画面から消えた時に呼び出される。*/
       public void stop() {
             if( flipThread != null ) {
                   flipThread.stop();
                   flipThread = null;
             }
       }

     /** アプレットが続行する作業 */
       public void run() {
             while( flipThread.isAlive() ) {
                  repaint();
                  if( showMode != 0 ) {    // 回転の表示中
                      try { Thread.sleep( intervalTime*2/5 ); }
                      catch (InterruptedException e){}
                      showMode++;
                      showMode%=4;
                  }
                  else {       // イメージの表示中
                      try { Thread.sleep( intervalTime ); }
                      catch (InterruptedException e){}
                      counter++; counter%=maxNumber;
                      showMode = 1;
                  }
             }
       }
}