import java.applet.AppletContext;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.util.Random;

public class animBtn extends Applet implements Runnable
{
    int frameNo, oldFrame;
    Image imgAnim;
    AudioClip theClip;
    Thread theThread;
    int theWidth;
    int theHeight;
    int speed;
    URL hRef;
    int state1 = 0;
    int step1, step2, step3;
    String frame;

    public void init()
    {
        String string;
        speed = 1000 / Integer.parseInt(getPara("fps", "10"));
        theWidth = size().width;
        theHeight = size().height;
        frame = getPara("target", "_top");
        string = getParameter("URL");
        if (string != null)
                try
                {
                    hRef = new URL(getDocumentBase(), string);
                }
                catch (MalformedURLException e)
                {
                    getAppletContext().showStatus("Bad URL: " + string);
                    return;
                }

        step1 = Integer.parseInt(getPara("step1", "1"));
        step2 = Integer.parseInt(getPara("step2", "1"));
        step3 = Integer.parseInt(getPara("step3", "1"));

        String string1 = getPara("images", "anim.jpg");
        imgAnim = getImage(getDocumentBase(), string1);
        String string2 = getParameter("sound");
        if (string2 != null)
            theClip = getAudioClip(getDocumentBase(), string2);
    }

    String getPara(String string1, String string2)
    {
        String string3 = getParameter(string1);
        if (string3 == null)
            string3 = string2;
        return string3;
    }

    public void start()
    {
        oldFrame = 0;
        frameNo = 0;
        if (theThread == null)
        {
            theThread = new Thread(this);
            theThread.start();
        }
    }

    public void stop()
    {
        if (theThread != null)
        {
            theThread.stop();
            theThread = null;
        }
        frameNo = 0;
    }
    public void run()
    {
        Random random = new Random();
        setBackground(Color.white);
        while (speed != 0)
        {
            nextFrame ();

            try
            {
                Thread.sleep((long)speed);
            }
            catch (InterruptedException e)
            {
            }
        }
    }

    synchronized void nextFrame () {
        if (++frameNo == step1)
            frameNo = 0;
        else if (frameNo == (step1 + step2))
            frameNo = step1;
        else if (frameNo == (step1 + step2 + step3))
            frameNo = step1 + step2;
        if (frameNo != oldFrame)
            repaint();
        else
            stop ();
        oldFrame = frameNo;
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        g.drawImage(imgAnim, -theWidth * frameNo, 0, this);
    }

    public boolean mouseDown(Event event, int i, int j)
    {
        boolean flag = super.mouseDown(event, i, j);
        if (theClip != null)
            theClip.play();
        frameNo = step1 + step2;
        repaint();
        return true;
    }

    public boolean mouseUp(Event event, int i, int j)
    {
        boolean flag = super.mouseUp(event, i, j);
        frameNo = step1;
        repaint();
        showStatus("" + hRef);
        if (hRef != null) 
            getAppletContext().showDocument(hRef, frame);
        return true;
    }

    public boolean mouseEnter(Event event, int i, int j)
    {
        boolean flag = super.mouseEnter(event, i, j);
        frameNo = step1;
        repaint();
        return true;
    }

    public boolean mouseExit(Event event, int i, int j)
    {
        boolean flag = super.mouseExit(event, i, j);
        frameNo = 0;
        repaint();
        return true;
    }
}
