/* * File: AnimatedCyberPet.java * Author: Java, Java, Java * Description: This applet provides a Graphical User * Interface for the CyberPet class. It animates the * CyberPet's eating actions by looping through a * set sequence of images. The images simulate the pet * chewing its food. */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class AnimatedCyberPet extends Applet implements ActionListener { private final int PAUSE = 2000000; // Named constant // Instance variables. private CyberPet pet1 = new CyberPet("Socrates"); // CyberPet private Label nameLabel = new Label("Hi! My name is " // Label + pet1.getName() + " and currently I am : "); private TextField stateField = new TextField(12); // A TextField private Button eatButton = new Button("Eat!"); // Two Buttons private Button sleepButton = new Button("Sleep!"); private Image eatImg, eat2Img, sleepImg, happyImg; // Images for animation /** * init() -- sets up the applet's interface and loads the * images used in the animation */ public void init() { eatButton.addActionListener(this); // Assign the listeners to the buttons. sleepButton.addActionListener(this); stateField.setText( pet1.getState() ); // Initialize the TextField stateField.setEditable(false); add(nameLabel); // Add the components to the applet. add(stateField); add(eatButton); add(sleepButton); eatImg = getImage(getCodeBase(), "eatImage.gif"); // Load the images eat2Img = getImage(getCodeBase(), "eat2Image.gif"); sleepImg = getImage(getCodeBase(), "sleepImage.gif"); happyImg = getImage(getCodeBase(), "happyImage.gif"); setSize(300,300); // Set the applet's size } // init() /** * paint() -- paints an appropriate image based on CyberPet's state * @param g -- the applet's Graphics context */ public void paint(Graphics g) { String petState = pet1.getState(); if (petState.equals("Eating")) doEatAnimation(g); else if (petState.equals("Sleeping")) g.drawImage(sleepImg, 20, 100, this); } // paint() /** * doEatAnimation() -- loops through the five images that make up * the eating animation, pausing for a brief instant between each image * @param g -- the applet's Graphics context */ private void doEatAnimation(Graphics g) { for (int k = 0; k < 5; k++) { g.drawImage( eatImg ,20, 100, this); busyWait(PAUSE); g.drawImage(eat2Img, 20, 100, this); busyWait(PAUSE); } g.drawImage(happyImg, 20, 100, this); } // doEatAnimation() /** * busyWait() -- loops through N iterations, doing nothing. This is * called "busy waiting" because nothing else can be done while the * program is looping in this method. * @param N -- the wait loop bound */ private void busyWait(int N) { for (int k = 0; k < N; k++) ; // Empty for body --- does nothing } // busyWait() /** * actionPerformed() -- handles the applet's action events * @param e -- the ActionEvent the caused this method to be invoked */ public void actionPerformed(ActionEvent e) { if (e.getSource() == eatButton) pet1.eat(); else if (e.getSource() == sleepButton) pet1.sleep(); stateField.setText(pet1.getState()); repaint(); } // actionPerformed() } // AnimatedCyberPet