Neko the cat

Chapter: Neko the cat

We met Neko in the in-class exercises on Swing and Animation. I am told that "Neko" is Japanese for cat. Here it refers to a small animation written and drawn for the Macintosh by Kenji Gotoh in 1989. Since then, many people have worked on similar programs for all sorts of platforms. The rendition you see here is based on a version that appeared with "Teach Yourself Java in 21 Days" by Laura Lemay and Charles Perkins. I have added some randomization. The idea for putting several Nekos together as separate threads for a "cat race" originates with James Leutz, who worked on it as a final project for a class at Oberlin College in the Fall semester 1997.

Here is the Neko applet from Lemay and Perkins:

And here is the Java code. The images can be found at awake.gif, right2.gif, scratch2.gif, sleep2.gif, yawn.gif, right1.gif, scratch1.gif, sleep1.gif, stop.gif.


Exercise 1

Repetitive Neko

Just to familiarize yourself with the Neko code, try to put the animation into an infinite loop. The cat should repeatedly do its thing from left to right until you kill the window.


The cat code is pretty boring. It always runs to exactly half the width, stops, pauses yawns, scratches exactly four times, sleeps for exactly 5 units, and then runs the rest of the way at the same speed as its first spurt.


Exercise 2

Randomize the cat. Make it so it still gets across the screen, but make its movements unpredictable. To do this, you need to make ample use of Math.random(). It doesn't have to sleep every time, it doesn't have to scratch every time, its speed during a run can be random, the distance it runs does not have to be half a screen width. Just be sure the cat does eventually cross the whole screen.


rhyspj@gwu.edu