📄 commonanimator.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class CommonAnimator extends Applet implements Runnable,MouseListener{
int frameNumber;
int delay; //线程睡多少时间
int allFrame=3;//用于控制多少幅图象
Thread animatorThread;
Dimension offDimension;
Image offImage;
Graphics offGraphics;
Image images[];
MediaTracker tracker;
public void init(){
String str;
addMouseListener(this);
//How many milliseconds between frames?
str=getParameter("fps");//从ex.html中的<applet >...</applet>取得的参数
int fps=(str!=null)?Integer.parseInt(str):10;
delay=(fps>0)?(1000/fps):100;
//Load all the images.
images=new Image[allFrame];
tracker=new MediaTracker(this);
for(int i=1;i<=allFrame;i++){
images[i-1]=getImage(getCodeBase(),"T"+i+".jpg");
tracker.addImage(images[i-1],0);
}
}
public void start(){
if(animatorThread==null){
animatorThread=new Thread(this);
animatorThread.start();
//调用线程的start()方法,然后执行线程的run()方法
}
}
public void run(){
//Remember the starting time
long startTime=System.currentTimeMillis();//取得系统当前时间(秒)
while(Thread.currentThread()==animatorThread){
//Display the next frame of animator
repaint();
//Delay depending on how far we are behind.
try{
startTime+=delay;
Thread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
}catch(InterruptedException e){
break;
}
frameNumber++;
}
}
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){}
//Paint the previous frame(if any).
public void paint(Graphics g){
if(offImage!=null){
g.drawImage(offImage,0,0,this);
}
}
public void update(Graphics g){
Dimension d=getSize();
//Create the offscreen graphics context,if no good one exists.
if((offGraphics==null)||(d.width!=offDimension.width)||(d.height!=offDimension.height)){
offDimension=d;
offImage=createImage(d.width,d.height);
offGraphics=offImage.getGraphics();
}
//Erase the previous image.
offGraphics.setColor(getBackground());
offGraphics.fillRect(0,0,d.width,d.height);
offGraphics.setColor(Color.black);
//paint only if all images have arrived.
if(tracker.statusID(0,true)==MediaTracker.COMPLETE){
offGraphics.drawImage(images[frameNumber%allFrame],0,0,this);
}
//paint the image onto the screen.
g.drawImage(offImage,0,0,this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -