JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--JAVA技术--JAVA线程  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
采用多线程制作动画
作者:未知     文章来源:www.jspcn.net
访问次数:569次     加入时间:2005年01月19日
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;
} 
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号