import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.io.InputStream;
public class Animation extends Applet implements Runnable {
public void init() {
/* get image */
img=getImage(getDocumentBase(),getParameter("imagename")); imgCount=Integer.valueOf(getParameter("imagecount")).intValue(); setBackground(Color.white);
/* applet's component lay out */
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
start_button=new Button("Start");
stop_button=new Button("Stop");
stop_button.disable();
p.add(start_button);
p.add(stop_button);
add("South",p);
canvas=new Canvas();
add("Center",canvas);
}
public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
}catch(InterruptedException e){}
++currentImg;
if(currentImg==imgCount) currentImg=0;
}
}
public void stop() {
if(animation!=null) {
animation.stop();
animation=null;
}
}
public void paint(Graphics g) {
Graphics c=canvas.getGraphics();
if(ImgWidth<=0||ImgHeight<=0) {
Image buffer=createImage(1,1);
Graphics bg=buffer.getGraphics();
bg.drawImage(img,0,0,this);
bg.dispose(); return;
}
c.drawImage(img,0,-(ImgHeight/imgCount)*currentImg,null); c.dispose();
}
public boolean imageUpdate(Image image,int infoflags,int x,int y,int width,int height) {
if((infoflags&ImageObserver.ALLBITS)!=0) {
ImgWidth=img.getWidth(null);
ImgHeight=img.getHeight(null);
repaint();
return false;
} return true;
}
public boolean action(Event evt,Object arg) {
if(arg.equals("Start")) {
start_button.disable();
stop_button.enable();
animation=new Thread(this);
animation.start();
}
else {
if(arg.equals("Stop")) {
stop_button.disable();
start_button.enable();
animation.stop();
}
else return super.action(evt,arg);
}
return true;
}
private Image img;
private Thread animation;
private int currentImg=0;
private int imgCount;
private Button start_button,stop_button;
private Canvas canvas;
private int ImgWidth=0;
private int ImgHeight=0;
}