码迷,mamicode.com
首页 > 移动开发 > 详细

e558. 在Applet中多图片交互显示

时间:2018-09-02 21:53:02      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:cti   extend   table   nat   oid   this   applet   extends   practice   

This is the simplest applet to animate an array of images. In practice, you should use double-buffering (which this example does not use) to eliminate flickering.

    import java.applet.*;
    import java.awt.*;
    
    public class AnimApplet extends Applet implements Runnable {
        Image[] images = new Image[2];
        int frame = 0;
        volatile Thread thread;
    
        public void init() {
            images[0] = getImage(getDocumentBase(), "http://hostname/image0.gif");
            images[1] = getImage(getDocumentBase(), "http://hostname/image1.gif");
        }
        public void start() {
            (thread = new Thread(this)).start();
        }
        public void stop() {
            thread = null;
        }
        public void paint(Graphics g) {
            g.drawImage(images[frame], 0, 0, this);
        }
        public void run() {
            int delay = 1000;    // 1 second
            try {
                while (thread == Thread.currentThread()) {
                    frame = (frame+1)%images.length;
                    repaint();
                    Thread.sleep(delay);
                }
            } catch (Exception e) {
            }
        }
    }

 

Related Examples

e558. 在Applet中多图片交互显示

标签:cti   extend   table   nat   oid   this   applet   extends   practice   

原文地址:https://www.cnblogs.com/borter/p/9575304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!