import java.awt.*;
import java.applet.*;
//注意到这个程序和Hello程序有什么不一样吗?在这个程序中多了implements Runnable。
public class carton extends Applet implements Runnable
{
Image img;
Thread thd = null;
int i;
int imgWidth = 150;
int imgHeight = 150;
int ncyc=1 ;
String namestr[] = new String[5] ;
//当线程被激活时开始运行run()函数。
public void run()
{
for (int j=0; j<5; j++)
{
namestr[j] = Integer.toString(j,8)+".jpg" ;
}
ncyc = -1 ;
while (true)
{
if (ncyc<=3) ncyc= ncyc+1 ; //初始化循环控制参数
else ncyc = 0 ;
img = getImage(getCodeBase(), namestr[ncyc]) ;
if (img != null)
{
i=imgHeight;
//repaint();
}
try {Thread.sleep(1000);} catch (InterruptedException e){}
i=0;
while (i<imgHeight)
{
repaint();
try {Thread.sleep(50);} catch (InterruptedException e){}
i+=4;
}
}
}
//每次代码在新位置处重画位图,它都要调用repaint。该函数调用可重载的update方法。update方法与paint方法是相同的,这里为啥不用怕paint(Graphics g)呢?除了paint方法在绘图前要清除窗口,而update方法不清除(如果你把update方法改名为paint,你会看到有什么不同)。
public void update(Graphics g)
{
if (img != null)
{
g.clipRect(0, 0, imgWidth, i);
g.drawImage(img, 0, i - imgHeight, null);
}
}
public void start()
{
if (thd == null)
{
thd = new Thread(this);
thd.start();
}
}
public void stop()
{
thd = null;
今日更新的文章
与 "采用多线程制作动画" 相关的文章
- 暂无相关文章
让Ajax实现提示框- · 弹出提示几种方法
- · Ajax开发聊天系统
- · Ajax开发实例教程
- · XMLHttpRequest
- · 推送方式拉取方式
SAAJ解决SOA集成问题- · 框架设计模式介绍
- · Validator 验框架
- · Struts配置文件解
- · 框架配置文件实例
- · 解释器设计模式
- · 学习Java构造函数
- · Comparator的使用
- · Java相关原理理解
- · Java专业术语规范
- · Java编程学习总结
- · 浅析Java事件处理
- · Java类型提升约定
- · 抽象类和接口比较
- · Java占用内存研究
- · Java实现回调例程
- · Java线程概念描述
- · Java反射机制详解
- · Java 操作Cookie
- · 设计 Annotation
- · 初学者搞懂的问题
- · Java30个基本规则
- · Acegi 框架技术
- · 三层架构配置入门
- · 随机验证码实例
- · 使用Volatile变量