import java.applet.*; import java.awt.*; import java.awt.event.*; public class CatMouseChase extends Applet implements ActionListener { public void init ( ) { super.init ( ); nextStep = new Button ("Move once"); add (nextStep); nextStep.addActionListener (this); ourWindowWidth = getIntegerParam ("width"); ourWindowHeight = getIntegerParam ("height"); myXCenter = ourWindowWidth/2; myYCenter = ourWindowHeight/2; int smallestDimension = ourWindowWidth < ourWindowHeight? ourWindowWidth: ourWindowHeight; double mouseAngle = getDoubleParam ("mouseangle"); double catRadius = getDoubleParam ("catradius"); double catAngle = getDoubleParam ("catangle"); myUnit = (smallestDimension/2 - BORDER)/((int) (catRadius+1.0)); myCat = new Cat (new Position (catRadius, catAngle)); myMouse = new Mouse (new Position (1.0, mouseAngle)); statusString = "Chase is in progress."; } private int getIntegerParam (String name) { String value = this.getParameter (name); if (value == "") { return 0; } try { return Integer.parseInt (value); } catch (Exception e) { return 0; } } private double getDoubleParam (String name) { String value = this.getParameter (name); if (value == "") { return 0.0; } try { return Double.valueOf (value).doubleValue ( ); } catch (Exception e) { return 0.0; } } public void actionPerformed (ActionEvent event) { // You fill this in. } public void paint (Graphics g) { // The magic numbers used here should really depend on the height of the // type face used to display the window annotations. // We'll fix that for next semester. g.drawOval (myXCenter+5-myUnit, myYCenter+5-myUnit, 2*myUnit-10, 2*myUnit-10); g.drawString ("C", myCat.getPosition ( ).xCoord (myXCenter, myUnit)-5, myCat.getPosition ( ).yCoord (myYCenter, myUnit)+5 ); g.drawString ("M", myMouse.getPosition ( ).xCoord (myXCenter, myUnit)-5, myMouse.getPosition ( ).yCoord (myYCenter, myUnit)+5 ); g.drawString ("Mouse at " + myMouse.getPosition ( ) + "; cat at " + myCat.getPosition ( ), 50, ourWindowHeight-30); g.drawString (statusString, 50, ourWindowHeight-15); } private Cat myCat; private Mouse myMouse; private boolean chaseIsOver = false; private int timeElapsed; private final int TIME_LIMIT = 30; private Button nextStep; private String statusString = "Not yet started."; private final int BORDER = 50; // margin for buttons and status strings private static int ourWindowWidth, ourWindowHeight; private int myXCenter, myYCenter, myUnit; }