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;
}
闽公网安备 35060202000074号