Pages

2011年2月19日 星期六

JaveSE Sample

圖片連續播放範例 - 使用 Applet
import java.awt.Image ;
import java.awt.Graphics ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.applet.Applet ;
import javax.swing.Timer ; // 利用 Timer 觸發事件

public class Animation extends Applet implements ActionListener {

Image[] imgArray ;
Timer timer ;
final int imgPerSecond = 10 ;
final int latency = 10000/imgPerSecond ; // 延遲時間單位為毫秒
int id = 0 ;

public void init() {
imgArray = new Image[imgPerSecond] ;
for( int i = 0 ; i < color="#666666">// 逐一載入影像檔案
imgArray[i] = getImage( getCodeBase(), i+".gif" ) ;
}

public void start() {
timer = new Timer( latency, this ) ; // 計時器時間到時會通知this(動畫測試)物件
timer.start(); // 啟動計時器
}

public void paint( Graphics g ) {
g.drawImage( imgArray[id], 0, 0, this ) ;
}

public void actionPerformed( ActionEvent event ) { // 計時器時間到時被呼叫的方法
id = ( id + 1 ) % imgPerSecond ;
repaint() ; // 此方法會清除繪圖區之後呼叫paint方法
}

}
使用時,在 Html 檔案中加入
其中 *.class 為要載入的 class 檔,寬高可自行設定。

沒有留言:

 
Blogger Templates