?? activeimages.java
字號:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ActiveImages extends Applet implements Runnable,ActionListener
{
Image iImages[]; //圖像數組
Thread aThread;
int iFrame; //圖像數組下標
int sleeptime; //參數,線程sleep時間
String graphfile; //圖片文件名
int graphcount; //圖片張數
AudioClip au; //定義一個聲音對象
Button b1,b2;
public void init()
{
int i,j;
iFrame=0;
aThread=null;
sleeptime=Integer.parseInt(getParameter("sleeptime"));
graphfile = getParameter("graphfile");
graphcount = Integer.parseInt(getParameter("graphcount"));
iImages = new Image[graphcount];
String fname = graphfile;
j = fname.indexOf(".");
for (i=0;i<graphcount;i++)
{
fname = fname.substring(0,j-1)+i+fname.substring(j);
iImages[i] = getImage(getDocumentBase(),"Images/"+fname);
}
au=getAudioClip(getDocumentBase(),"Wav/Sound.wav");
au.play(); //播放一次聲音文件
Panel p1 = new Panel();
b1 = new Button("Start");
b2 = new Button("Stop");
p1.add(b1);
p1.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setLayout(new BorderLayout());
add(p1,"South");
}
public void start()
{
if (aThread == null)
{
aThread = new Thread(this);
aThread.start(); //線程啟動
b1.setEnabled(false);
}
}
public void stop()
{
if (aThread != null)
{
aThread.interrupt(); //線程中斷
aThread = null;
au.stop(); //停止播放聲音文件
}
}
public void run()
{
while (true)
{
iFrame++;
iFrame %= (iImages.length); //下一幅圖像的下標
repaint();
try
{
Thread.sleep(sleeptime);
}
catch (InterruptedException e)
{ //中斷時拋出
break; //退出循環
}
}
}
public void update(Graphics g)
{
g.drawImage(iImages[iFrame],0,0,this);
}
public void actionPerformed(ActionEvent e)
{
if ((e.getSource()==b1) && (aThread == null) )
{ //單擊Start按鈕時觸發
aThread = new Thread(this);
aThread.start(); //線程啟動
b1.setEnabled(false);
b2.setEnabled(true);
au.loop(); //循環播放聲音文件
}
if ((e.getSource()==b2) && (aThread != null) )
{ //單擊Stop按鈕時觸發
aThread.interrupt(); //線程中斷
aThread = null;
b1.setEnabled(true);
b2.setEnabled(false);
au.stop(); //停止播放聲音文件
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -