📄 activeimagesjapplet.java
字号:
//【例8.6】 图像的动画设计。
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
public class ActiveImagesJApplet extends JApplet implements Runnable,ActionListener
{
private Image[] images; //图像数组
private int index; //图像数组下标
private Thread athread; //线程
private int sleeptime; //线程sleep时间
private String graphfile; //图像文件名
private int graphcount; //图像张数
private JButton button_start,button_stop; //启动和停止按钮
public void init()
{
this.index=0;
this.athread=null;
this.sleeptime = Integer.parseInt(this.getParameter("sleeptime")); //获得参数
this.graphfile = this.getParameter("graphfile");
this.graphcount = Integer.parseInt(this.getParameter("graphcount"));
this.images = new Image[this.graphcount];
String fname = this.graphfile;
int j = fname.indexOf(".");
for (int i=0;i<this.graphcount;i++) //所有图像装入数组
{
fname = fname.substring(0,j-1)+i+fname.substring(j); //拼接其他图像的文件名
images[i] = this.getImage(this.getDocumentBase(),"Images/"+fname);
}
JPanel panel = new JPanel();
button_start = new JButton("Start");
panel.add(button_start);
button_start.addActionListener(this);
button_stop = new JButton("Stop");
panel.add(button_stop);
button_stop.addActionListener(this);
this.setLayout(new BorderLayout());
this.add(panel,"South");
}
public void update(Graphics g)
{
g.drawImage(this.images[this.index],0,0,this);
}
public void start()
{
if (athread == null)
{
athread = new Thread(this);
athread.start(); //线程启动
button_start.setEnabled(false);
button_stop.setEnabled(true);
}
}
public void stop()
{
if (athread != null)
{
athread.interrupt(); //线程中断
athread = null;
button_start.setEnabled(true);
button_stop.setEnabled(false);
}
}
public void run()
{
while (true)
{
index = (index+1) % images.length; //下一幅图像的下标
repaint();
try
{
athread.sleep(this.sleeptime);
}
catch (InterruptedException e)
{ //中断时抛出
break; //退出循环
}
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button_start) //单击Start按钮时
this.start(); //调用执行当前Applet对象的start()方法
if (e.getSource()==button_stop) //单击Stop按钮时
this.stop(); //调用执行当前Applet对象的stop()方法
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -