import java.awt.*; public class Penguin extends Thread { ActionList action; public void run() { while (true) { perform(action.get()); } } private void _pause() { try { this.sleep(speed * 10); } catch (InterruptedException e) { } } private void _think() { try { this.sleep(100); } catch (InterruptedException e) { } } private void _turnLeft() { _think(); if (direction.equals("south")) { direction = "east"; look = 12; } else if (direction.equals("east")) { direction = "north"; look = 5; } else if (direction.equals("north")) { direction = "west"; look = 7; } else { direction = "south"; look = 2; } report(); } private void _wave() { for (int i = 0; i < 4; i++) { _think(); look = 0; report(); _think(); look = 39; report(); } _think(); look = 0; report(); } private void _happy() { _think(); if (direction.equals("south")) { _wave(); _think(); look = 2; report(); } else if (direction.equals("east")) { look = 2; _wave(); _think(); look = 12; report(); } else if (direction.equals("north")) { look = 7; report(); _think(); look = 2; report(); _wave(); _think(); look = 7; report(); _think(); look = 5; report(); _think(); } else { // west look = 2; report(); _wave(); look = 7; report(); _think(); } } private void _turnRight() { _think(); if (direction.equals("south")) { direction = "west"; look = 7; } else if (direction.equals("east")) { direction = "south"; look = 2; } else if (direction.equals("north")) { direction = "east"; look = 12; } else { direction = "north"; look = 5; } report(); } void _moveForward() { for (int i = 0; i < 5; i++) { _think(); if (direction.equals("south")) { y += dy; look = animP[12 + (i + 1) % 4]; } else if (direction.equals("east")) { x += dx; look = animP[ 4 + (i + 2) % 4]; } else if (direction.equals("north")) { y -= dy; look = animP[ 8 + (i + 1) % 4]; } else { // west x -= dx; look = animP[ 0 + i % 4]; } report(); } } public void perform(String action) { if (action.equals("turnLeft")) { this._turnLeft(); } else if (action.equals("turnRight")) { this._turnRight(); } else if (action.equals("moveForward")) { this._moveForward(); } else if (action.equals("backwards")) { _turnLeft(); _turnLeft(); _moveForward(); _turnRight(); _turnRight(); } else if (action.equals("pause")) { this._pause(); } else if (action.equals("think")) { this._think(); } else if (action.equals("happy")) { this._happy(); } else System.out.println("Don't understand " + action); } void turnRight() { action.put("turnRight"); } void moveForward() { action.put("moveForward"); } void wave() { action.put("wave"); } void pause() { action.put("pause"); } void think() { action.put("think"); } void happy() { action.put("happy"); } int animP[] = { 7, 8, 9, 8, // left ( west) 10, 11, 12, 11, // right ( east) 4, 5, 6, 5, // up (north) 1, 2, 3, 2 // down (south) }; Rink location; void placeIn(Rink placement, int x, int y) { this.location = placement; frames = this.location.small; this.x = x * 30; this.y = y * 30; this.report(); } Image[] frames; void report() { location.repaint(); } int x, y, dx = 6, dy = 6, look = 2; void draw(Graphics g) { g.drawImage(frames[look], x, y, location); } int speed = 100; Penguin() { action = new ActionList(); this.speed = 100; } void turnLeft() { action.put("turnLeft"); } String direction = "south"; }