/* Neko the Cat! Modification of Lemay's Neko in TYJ in 21 Days. I broke the program into two specific threads - Applet and NekoThread. This thread has 5 methods: pause, nekorun, nekoscratch, nekosleep and run. They all call moveNeko from our Applet to place their specific cat image on the screen. */ import java.awt.Image; import Neko; public class NekoThread extends Thread { Image nekopics[] = new Image[9]; String nekosrc[] = { "right1.gif", "right2.gif", "stop.gif", "yawn.gif", "scratch1.gif", "scratch2.gif", "sleep1.gif", "sleep2.gif", "awake.gif" }; int x; int y = 50; int ni; // Neko Index = which image is currently displayed Neko n; public NekoThread(Neko n) { this.n = n; for (int i=0; i < nekopics.length; i++) { nekopics[i] = n.getImage(n.getDocumentBase(), "images/" + nekosrc[i]); } } void pause(int time) { try { sleep(time); } catch (InterruptedException e) { } } void nekorun(int start, int end) { ni = 0; // We always start a run with "right1.gif" for (x = start; x < end; x += 10) { n.moveNeko(nekopics[ni], x, y); if (ni == 0) // swap images ni = 1; else ni = 0; pause(150); } } void nekoscratch(int numtimes) { for (int i = numtimes; i > 0; i--) { n.moveNeko(nekopics[4], x, y); pause(150); n.moveNeko(nekopics[5], x, y); pause(150); } } void nekosleep(int numtimes) { for (int i = numtimes; i > 0; i--) { n.moveNeko(nekopics[6], x, y); pause(250); n.moveNeko(nekopics[7], x, y); pause(250); } } public void run() { while (true) { // run from one side of the screen to the middle nekorun(0, n.size().width / 2); // stop and pause n.moveNeko(nekopics[2], x, y); pause(1000); // yawn n.moveNeko(nekopics[3], x, y); pause(1000); // scratch four times nekoscratch(4); // sleep for 5 seconds nekosleep(5); // wake up n.moveNeko(nekopics[8], x, y); pause(500); // run off screen nekorun(x, n.size().width + 10); } } }