import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Component;
public class Animate extends JApplet {
//图片数量
private static final int NUM_OF_PIC = 5;
int count;
Image pics[];
TimerThread timer;
public void init(){
count = 1;
pics = new Image[NUM_OF_PIC];
MediaTracker tracker = new MediaTracker(this);
for(int i = 0; i<NUM_OF_PIC; i++){
//将图片按照0,1,...,NUM_OF_PIC -1,放置在目录中,格式为.jpg
pics[i] = getImage(getCodeBase(), new Integer(i).toString()+".jpg");
tracker.addImage(pics[i], 0);
}
tracker.checkAll(true);
}

public void start(){
timer = new TimerThread(this, 1000);
timer.start();
}

public void stop() {
timer.shouldRun = false;
try{
timer.join();
//等待timer线程退出
}
catch (InterruptedException e){};
}

public void paint(Graphics g){
g.drawImage(pics[count++], 0, 0, null);
if(count == NUM_OF_PIC) count = 0;
}
}class TimerThread extends Thread{
Component comp;
int timediff;
// shouldRun声明为volatile
volatile boolean shouldRun;
public TimerThread(Component comp, int timediff){
super("TimerThread(" + timediff + " millseconds");
this.comp = comp;
this.timediff = timediff;
shouldRun = true;
}
public void run(){
while(shouldRun){
try{
comp.repaint();
sleep(timediff);
}
catch (Exception e){}
}
}
}
        这段代码是从网上找来的,我弄到我机子上运行了一下,Applet是启动了,但没达到预期效果啊!有没高人指点下!图片都放在代码同一个目录下了,都是jpg格式的,名字是0,1,2,3,4.不知道是不是因为图片大小不一样所以没反应呢!!!我用的是eclips 6.5企业版。
还有,怎么把这个Applet嵌入到网页上呢?

解决方案 »

  1.   

    dos 窗口下你用appletviewer Animate.java行动你的程序
    或者:
    你自己写一个小的index.htm文件,文件内容:
    <applet code=Animate width=200 height=200>
    </applet>
      

  2.   

    dos 窗口下你用appletviewer Animate起动你的程序 
    或者: 
    你自己写一个小的index.htm文件,文件内容: 
    <applet code=Animate(package name: com.Animate) width=200 height=200> 
    </applet>
      

  3.   

    2楼的好无聊哦!!
    有那位高手能帮我写个功能和上面代码一样功能的Applet给我吗,我对线程编程不熟!
      

  4.   

    好像应该是
    <applet code="Animate.java" width=200 height=200>
    </applet>
      

  5.   

    你看这样能不能达到你的要求。
    import javax.swing.JApplet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Component;
    public class Animate extends JApplet {
        //图片数量
        private static final int NUM_OF_PIC = 5;
        int count;
        Image pics[];
        TimerThread timer;
        public void init(){
            count = 1;
            pics = new Image[NUM_OF_PIC];
            MediaTracker tracker = new MediaTracker(this);
            for(int i = 0; i<NUM_OF_PIC; i++){
                //将图片按照0,1,...,NUM_OF_PIC -1,放置在目录中,格式为.jpg
                pics[i] = getImage(getCodeBase(), new Integer(i).toString()+".jpg");
                tracker.addImage(pics[i], 0);
            }
            tracker.checkAll(true);
        }
        
        public void start(){
            timer = new TimerThread(this, 1000);
            timer.start();
        }
        
        public void stop() {
            timer.shouldRun = false;
            try{
                timer.join();
                //等待timer线程退出
            }
            catch (InterruptedException e){};
        }
        
        public void paint(Graphics g){
            g.drawImage(pics[count++], 0, 0, null);
            if(count == NUM_OF_PIC) count = 0;
        }
    }class TimerThread extends Thread{
        Component comp;
        int timediff;
        long time1=System.currentTimeMillis();
        long time2=System.currentTimeMillis();
        // shouldRun声明为volatile
        volatile boolean shouldRun;
        public TimerThread(Component comp, int timediff){
            super("TimerThread(" + timediff + " millseconds");
            this.comp = comp;
            this.timediff = timediff;
            shouldRun = true;
        }
        public void run(){
         if(time2-time1>=5000) 
    shouldRun=false;
           else 
            {
              shouldRun=true;
                try{
                    comp.repaint();
                    sleep(timediff);
                }
                catch (Exception e){}
            }
        }
    }