MoveAnime.java
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Event;
/** キャラクターが横に移動しながらアニメーションする MoveAnime クラス */
public class MoveAnime [ ] [ ] [ ] [ ] {
/** イメージの数の上限 */
public static final int limitNumber=32;
/** アニメーションを操作するのスレッド */
public [ ] animater=null;
/** アニメーションのイメージ */
public [ ] 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 velocityValue=4;
/** アプレット内のイメージの位置 */
protected int x, y;
/** 初期設定のメソッド */
public void [ ]() {
// パラメータの読み込み
String dir = getParameter("dir");
if ( dir == null ) dir = "anime";
String file = getParameter("file");
if ( file == null ) file = "anime";
// イメージの読み込みを開始
images = new Image[limitNumber];
imageStates = new int[limitNumber];
for( int i=0; i<limitNumber; i++ ) {
images[i] = [ ]( getDocumentBase(),
dir + "/" + file + (i+1) + ".gif" );
}
x = 0; y = 0;
}
/** 画面表示(初期)のメソッド */
public void paint( Graphics g ) {
// イメージを生成するために描画命令が必要(省略不可)
for( int i=0; i<limitNumber; i++ ) {
g.[ ]( images[i],
size().width, size().height, this );
}
}
/** 画面表示のメソッド */
public void [ ]( 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 = [ ] Thread(this);
animater.[ ]();
}
}
/** 画面から消えた時に呼ばれる */
public void stop() {
if( animater != null ) {
animater.[ ]();
animater = [ ];
}
}
/** スレッドの処理の内容 */
public void [ ]() {
while( animater.isAlive() ) {
if( waitFlag) { // イメージの読み込みを監視
checkStates();
try { Thread.[ ]( 1000 ); }
catch (InterruptedException e){}
}
else { // アニメーションを開始
repaint();
counter = ( counter + 1 ) % maxNumber;
x = ( x + velocityValue ) % size().width;
try { Thread.[ ]( intervalTime ); }
catch (InterruptedException e){}
}
}
}
/** イメージの状態を調べるメソッド */
protected void checkStates() {
// ALLBITS--読み込みが完了 ERROR--読み込めない
boolean flag = false;
for( int i=0; i<limitNumber ; i++ ) {
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 [ ]( Event evt, int x, int y ) {
if( waitFlag ) return true; // まだイメージを読み込み中
if( animater == null ) {
animater = [ ] Thread(this);
animater.[ ]();
}
else {
animater.[ ]();
animater = [ ];
}
return true;
}
}