001 /*
002 * CatPanel.java
003 * Part of the CatAndMouse problem set.
004 *
005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006 * For more information, see http://www.ai.mit.edu/projects/cs101, the
007 * CS101 homepage or email las@ai.mit.edu.
008 *
009 * Copyright (C) 1998 Massachusetts Institute of Technology.
010 * Please do not redistribute without obtaining permission.
011 */
012
013 package catandmouse;
014
015 import java.awt.*;
016 import java.awt.event.*;
017 import cs101.net.*;
018
019 /**
020 * CatPanel displays the cat and mouse. It has methods to handle
021 * moving and clicking the mouse, and also has accessor methods so
022 * that the Mouse's coordinates can be updated.
023 *
024 * <P>Copyright (c) 1998 Massachusetts Institute of Technology
025 *
026 * @author Lynn Andrea Stein, las@ai.mit.edu
027 * @author Todd C. Parnell, tparnell@ai.mit.edu
028 * @author Henry Wong, henryw@mit.edu
029 * @version $Id: CatPanel.java,v 1.1.1.1 2002/06/05 21:56:34 root Exp $
030 */
031 public class CatPanel extends Panel implements Runnable {
032
033 /* In order to draw the mouse on screen, the panel has to
034 store the mouse's coordinates. */
035 private int mouseX = 0;
036 private int mouseY = 0;
037
038 /* The panel also has to store the cat's coordinates. */
039 private int catX = 0;
040 private int catY = 0;
041
042 /** the network communications abstraction */
043 private Wire myWire;
044
045 /* fields necessary for runnable */
046 private Thread spirit;
047 private boolean stopped;
048
049 /** initilizes the GUI and starts networking with a new ClientWire */
050 public CatPanel() {
051 this(new ClientWire());
052 }
053
054 /** initilizes the GUI and starts the networking with the given wire */
055 public CatPanel (Wire theWire) {
056 this.myWire = theWire;
057 this.addMouseMotionListener(new MouseMotionListener() {
058 public void mouseMoved(MouseEvent e) {
059 catX = e.getX();
060 catY = e.getY();
061 myWire.writeObject(new Packet (Packet.CATLOC,catX,catY));
062 repaint();
063 }
064 public void mouseDragged(MouseEvent e){}
065 });
066
067 this.addMouseListener(new MouseListener() {
068 public void mousePressed(MouseEvent e) {
069 int x = e.getX();
070 int y = e.getY();
071
072 boolean Xin =
073 ((mouseX - x <CatDemo.MOUSESIZE+CatDemo.CATSIZE)
074 && (x - mouseX<CatDemo.MOUSESIZE+CatDemo.CATSIZE));
075 boolean Yin =
076 ((mouseY - y < CatDemo.MOUSESIZE + CatDemo.CATSIZE)
077 && (y - mouseY<CatDemo.MOUSESIZE+CatDemo.CATSIZE));
078
079 if (Xin && Yin) {
080 myWire.writeObject(new Packet (Packet.CAT_HIT,x,y));
081 System.out.println ("A HIT occured at (" + x + "," + y + ")");
082
083 } else {
084 myWire.writeObject(new Packet (Packet.CAT_MISS,x,y));
085 System.out.println ("A MISS occured ar (" + x + "," + y + ")");
086 }
087 }
088
089 public void mouseReleased(MouseEvent e) {}
090 public void mouseClicked(MouseEvent e) {}
091 public void mouseEntered(MouseEvent e) {}
092 public void mouseExited(MouseEvent e) {}
093 });
094
095 // start recieving packets from the Mouse
096 this.spirit = new Thread(this);
097 this.spirit.start();
098 }
099
100 // The following 2 methods just help the GUI work correctly.
101 public Dimension getMinimumSize() {
102 return new Dimension (200, 200);
103 }
104 public Dimension getPreferredSize () {
105 return new Dimension (400,400);
106 }
107
108 /** A white circle represents the Mouse and a black circle
109 represents the Cat. */
110 public synchronized void paint (Graphics g) {
111 g.setColor (Color.red);
112 g.fillOval (this.mouseX-CatDemo.MOUSESIZE/2,
113 this.mouseY-CatDemo.MOUSESIZE/2,
114 CatDemo.MOUSESIZE, CatDemo.MOUSESIZE);
115
116 g.setColor (Color.black);
117 g.fillOval (this.catX-CatDemo.CATSIZE/2, this.catY-CatDemo.CATSIZE,
118 CatDemo.CATSIZE, CatDemo.CATSIZE);
119 }
120
121 /** called by the run method */
122 private synchronized void setMouseCoords (int x, int y) {
123 this.mouseX = x;
124 this.mouseY = y;
125 }
126
127 /**
128 * This method gets Packets from the Mouse via the network. It then
129 * calls setMouseCoords and repaint to update the GUI with the new
130 * location.
131 */
132 public void run () {
133 while ( !this.stopped ) {
134 try {
135 Packet mouseLoc = (Packet)myWire.readObject();
136 this.setMouseCoords(mouseLoc.getX(), mouseLoc.getY());
137 } catch (ClassCastException cce) {
138 System.out.println("Recieved bad Packet from mouse");
139 } catch (Exception e) {
140 System.out.println("Error reading from wire");
141 System.exit(0);
142 }
143 this.repaint();
144 Thread.yield();
145 }
146 }
147 }
148
149 /*
150 * $Log: CatPanel.java,v $
151 * Revision 1.1.1.1 2002/06/05 21:56:34 root
152 * CS101 comes to Olin finally.
153 *
154 * Revision 1.1 2000/05/06 18:41:39 mharder
155 * Moved to catandmouse subdirectory, changed package name
156 *
157 * Revision 1.5 1998/07/24 16:40:14 tparnell
158 * Placate new javadoc behavior
159 *
160 * Revision 1.4 1998/07/22 17:53:44 tparnell
161 * changes to use cs101.util, cs101.awt, etc.
162 *
163 * Revision 1.3 1998/07/20 16:24:07 tparnell
164 * Major cleanup of existing code. Moved *Thread into *Panel. Removed
165 * files that were duplicates of things in cs101.util. Moved files that
166 * were here but belonged in cs101.util there. Added javadoc and logging.
167 *
168 */
169
170