用Applet动画实现一个简单的Applet影集

public class ImageType extends Applet{

    int num=5;
    Image imgs[];
    public void init() {
        imgs=new Image[num];
        for(int i=0;i<num;i++) {
            imgs[i]=getImage(getDocumentBase(),"images/"+"t"+(i+1)+".gif");
            //在.class文件的路径下即bin目录下,创建一个images文件夹,再把图片放进去
        }
        this.setBackground(Color.white);
    }
    public void paint(Graphics g) {
        while(true) {
            for(int i=0;i<num;i++) { 
                g.drawImage(imgs[i], 0, 0, this);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {  //中断异常
                    e.printStackTrace(); 
                    //在命令行打印异常信息在程序中出错的位置及原因
                }
                g.clipRect(0, 0, getBounds().width, getBounds().height);
                //g.clipRect擦除原来的内容
                //getBounds().width, getBounds().height图片资源的宽高度
            }
        }
    }
}
 


运行结果:

猜你喜欢

转载自blog.csdn.net/xxx_1_1/article/details/82497710
今日推荐