?? 例8-14.java
字號(hào):
//Example 8-14
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class tt extends Applet implements Runnable,MouseListener
{
int frameNumber,delay,allFrame=10;
Thread animatorThread;
Dimension offDimension;
Image offImage,images[];
Graphics offGraphics;
MediaTracker tracker;
String s;
public void init()
{
String str;
addMouseListener(this);
str=getParameter("fps");
int fps=(str!=null)?Integer.parseInt(str):10;
delay=(fps>0)?(1000/fps):100;
images=new Image[allFrame];
tracker=new MediaTracker(this);
for(int i=0;i<allFrame;i++)
{
images[i]=getImage(getCodeBase(),"T"+i+".gif");
tracker.addImage(images[i],0);
}
}
public void start()
{
if(animatorThread==null)
{
animatorThread=new Thread(this);
animatorThread.start();
}
}
public void stop()
{
animatorThread=null;
offImage=null;
offGraphics=null;
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e)
{
if(animatorThread==null)
start();
else
animatorThread=null;
}
public void mouseReleased(MouseEvent e) { }
public void run()
{
long startTime=System.currentTimeMillis();
while(Thread.currentThread()==animatorThread)
{
repaint();
try
{
startTime+=delay;
Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
}
catch(InterruptedException e)
{
break;
}
frameNumber++;
}
}
public void paint(Graphics g)
{
if(offImage!=null)
g.drawImage(offImage,0,0,this);
}
public void update(Graphics g)
{
Dimension d=getSize();
if((offGraphics==null)||(d.width!=offDimension.width)||(d.height!=offDimension.height))
{
offDimension=d;
offImage=createImage(d.width,d.height);
offGraphics=offImage.getGraphics();
}
offGraphics.setColor(getBackground());
offGraphics.fillRect(0,0,d.width,d.height);
offGraphics.setColor(Color.black);
if(tracker.statusID(0,true)==MediaTracker.COMPLETE)
{
offGraphics.drawImage(images[frameNumber%allFrame],0,0,this);
}
g.drawImage(offImage,0,0,this);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -