MoveAnime.java



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

/** キャラクターが横に移動しながらアニメーションする MoveAnime クラス */

public class MoveAnime extends Applet implements Runnable {

    /** イメージの数の上限 */

       public static final int limitNumber=32;

    /** アニメーションを操作するのスレッド */

       public Thread animater=null;

    /** アニメーションのイメージの配列 */

       public Image images[];

    /** イメージの読み込み状態 */

       public int imageStates[];

    /** イメージの読み込みをしているか */

       public boolean waitFlag=true;

    /** イメージの数とサイズの最大値 */

       public int maxNumber=0, maxWidth=0, maxHeight=0;

    /** 表示すべきイメージの番号 */

       protected int counter=0;

    /** コマ送りのインターバル(ミリ秒単位) */

       protected int intervalTime=160;

    /** アプレット内のイメージの位置 */

       protected int x, y;

    /** アプレット内のイメージの位置 */

       protected int maxX;

    /** 画面上の移動の幅(ドット単位) */

       protected int velocityValue=4;

    /** 初期設定のメソッド */

       public void init() {

         // パラメータの読み込み

             String filename[] = new String[limitNumber];
             for( int i=0; i<limitNumber; i++ )
                  filename[i] = getParameter( "file" + (i+1) );

        // イメージの読み込みを開始

             images = new Image[limitNumber];
             imageStates = new int[limitNumber];
             for( int i=0; i<limitNumber; i++ )
                if( filename[i] != null )
                   images[i] = getImage( getDocumentBase(), filename[i] );

             x = 0;  y = 0;   // イメージの最初の位置
       }

    /** 画面表示(初期)のメソッド */

       public void paint( Graphics g ) {

        // イメージの生成を開始するために描画命令が必要(省略不可)

            for( int i=0; i<limitNumber; i++ )
               if( images[i] != null )
                 g.drawImage( images[i],
                              size().width, size().height, this );
       }

    /** 画面表示のメソッド */

       public void update( Graphics g ) {

             if( waitFlag ) return;  // まだイメージを読み込み中

             if( 0 <= counter && counter < maxNumber ) {
                  g.clearRect( x-velocityValue, y,
                               maxWidth+velocityValue, maxHeight );
                  g.drawImage( images[counter], x, y , this );
             }
       }

    /** 画面に現れた時に呼ばれる */

       public void start() {

             if( animater == null ) {
                 animater = new Thread(this);
                 animater.start();
             }
       }

    /** 画面から消えた時に呼ばれる */

       public void stop() {
             if( animater != null ) {
                   animater.stop();
                   animater = null;
             }
       }

    /** スレッドの処理の内容 */

       public void run() {

             while( animater.isAlive() ) {

              // イメージの読み込みを監視

                  if( waitFlag) {

                      checkStates();
                      try { Thread.sleep( 1000 ); }
                      catch (InterruptedException e){}
                  }

              // アニメーションを開始

                  else {
                      repaint();
                      counter = ( counter + 1 ) % maxNumber;
                      x = ( x + velocityValue ) % size().width;
                      try { Thread.sleep( intervalTime ); }
                      catch (InterruptedException e){}
                  }
             }
       }


    /** イメージの状態を調べる */

       protected void checkStates() {

        // ALLBITS--読み込みが完了  ERROR--読み込めない

            boolean flag = false;
            for( int i=0; i<limitNumber ; i++ ) {

               if( images[i] != null ) {
                   imageStates[i] = checkImage( images[i], this );
                   if( ( imageStates[i] & ALLBITS ) != ALLBITS 
                        && ( imageStates[i] & ERROR ) != ERROR ) {
                      flag = true;  // まだ状態が未確定
                      break;
                   }
               }
            }
            waitFlag = flag;

        // すべてのイメージの状態が確定した時に行う処理

            if( !waitFlag ) {
                for( int i=0; i<limitNumber ; i++ ) {
                     if( ( imageStates[i] & ALLBITS ) == ALLBITS ) {
                          maxWidth  = Math.max( maxWidth,
                                          images[i].getWidth(this) );
                          maxHeight = Math.max( maxHeight,
                                          images[i].getHeight(this) );
                          maxNumber = i+1;
                      }
                 }
            }
       }

    /** マウスがクリックされた時の処理 */

       public boolean mouseDown( Event evt, int x, int y ) {

             if( waitFlag ) return true; //まだイメージを読み込み中

          // マウスのクリックでアニメーションの動きを切り替える

             if( animater == null ) {
                   animater = new Thread(this);
                   animater.start();
             }
             else {
                   animater.stop();
                   animater = null;
             }
             return true;
       }
}