GIFファイルのぱらぱらアニメ



// * @(#)Tumble.java      
// * @author      James Gosling

import java.io.*;
import java.applet.*;
import java.awt.*;
import java.net.*;

public class test007 extends Applet implements Runnable {
    int loopslot = 0;
    Thread kicker = null;
    int pause = 1000;
    int offset = 0;
    int off;
    int speed = 100;
    int nimgs = 15;
    Image imgs[];
    int maxWidth;

    public void init() {
        maxWidth = 0;
        setBackground(Color.black);
    }
    public void run() {
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY-1);
        imgs = new Image[nimgs];
        for (int i = 1; i < nimgs; i++) {
            imgs[i] = getImage(getDocumentBase(), "../gif/b" + i + ".gif");
        }
        Dimension d = size();
        if (nimgs > 1) {
            if (offset < 0) {
                off = d.width - maxWidth;
            }
            while (kicker != null) {
                if (++loopslot >= nimgs) {
                    loopslot = 0;
                    off += offset;
                    if (off < 0) {
                        off = d.width - maxWidth;
                    } else if (off + maxWidth > d.width) {
                        off = 0;
                    }
                }
                repaint();
                try {
                    Thread.sleep(speed + ((loopslot == nimgs - 1) ? pause : 0));
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }
    public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
        if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0) {
            if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] == img)) {
                repaint(100);
            }
        }
        return (flags & (ALLBITS|ERROR)) == 0;
    }
    public void paint(Graphics g) {
       if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] != null)) {
            g.drawImage(imgs[loopslot], off, 10, this);
        }
    }
    public void start() {
        if (kicker == null) {
            kicker = new Thread(this);
            kicker.start();
        }
    }
    public void stop() {
        if (kicker != null) {
            kicker.stop();
            kicker = null;
        }
    }
}