//Brad Culberson 04.23.98
//this is neko, but he has a coordination problem.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;

public class MyApplet15 extends java.applet.Applet
    implements Runnable {

    Image nekopics[] = new Image[9];
    Image currentimg;
    Thread runner;
    int xpos;
    int ypos = 50;

    public void init() {
        String nekosrc[] = { "awake.gif", "sleep2.gif",
            "sleep1.gif", "yawn.gif", "scratch2.gif",
            "scratch1.gif", "stop.gif", "right2.gif",
             "right1.gif"};

        for (int i=0; i < nekopics.length; i++) {
            nekopics[i] = getImage(getCodeBase(),
            "images/" + nekosrc[i]);
        }
    }

    public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }
    
    public void stop() {
        if (runner != null) {
            runner.stop();
            runner = null;
        }
    }

    public void run() {
        setBackground(Color.black);
	
        nekorun(0, size().width / 2);

        currentimg = nekopics[2];
        repaint();
        pause(1000);

        currentimg = nekopics[3];
        repaint();
        pause(1000);

        nekoscratch(4);

        nekosleep(5);

        currentimg = nekopics[8];
        repaint();
        pause(500);
        nekorun(xpos, size().width + 10);
    }

    void nekorun(int start, int end) {
        for (int i = start; i < end; i += 10) {
            xpos = i;

            if (currentimg == nekopics[0])
                currentimg = nekopics[1];
            else currentimg = nekopics[0];
                repaint();
            pause(150);
        }
    }


    void nekoscratch(int numtimes) {
        for (int i = numtimes; i > 0; i--) {
            currentimg = nekopics[4];
            repaint();
            pause(150);
            currentimg = nekopics[5];
            repaint();
            pause(150);
        }
    }

    void nekosleep(int numtimes) {
        for (int i = numtimes; i > 0; i--) {
            currentimg = nekopics[6];
            repaint();
            pause(250);
            currentimg = nekopics[7];
            repaint();
            pause(250);
        }
    }

    void pause(int time) {
        try { Thread.sleep(time); }
        catch (InterruptedException e) { }
    }

    public void paint(Graphics g) {
	g.setColor(Color.white);
	g.fillRect(0,35,1000,50);
	g.drawString("Neko is messed up...",0,100);
        if (currentimg != null)
            g.drawImage(currentimg, xpos, ypos, this);
    }
}
