001 /* 002 * SetPosGravListener.java 003 * Part of the Spirograph problem set 004 * 005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. 006 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the 007 * CS101 homepage</a> or email <las@ai.mit.edu>. 008 * 009 * Copyright (C) 1996 Massachusetts Institute of Technology. 010 * Please do not redistribute without obtaining permission. 011 */ 012 013 package spirograph; 014 015 import java.awt.*; 016 import java.awt.event.*; 017 018 /** This code implements a listener which listens to the DotPanel and 019 * waits for mouse clicks. Depending on what "mode" it is in, the 020 * SetPosGravListener will either add in a new gravitational source, 021 * reposition the dot or do nothing. The mode of this listener is change 022 * by some of the button listeners in the AdvEnv instance. 023 * 024 * <p>Copyright © 1998 Massachusetts Institute of Technology<br /> 025 * Copyright © 2003 Franklin W. Olin College of Engineering</p> 026 * 027 * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu 028 * @author Henry Wong, henryw@mit.edu 029 * @author Patrick G. Heck, gus.heck@olin.edu 030 * @version $Id: SetPosGravListener.java,v 1.3 2003/01/17 19:31:08 gus Exp $ 031 * @see Coord 032 * @see DotPanel 033 */ 034 public class SetPosGravListener implements MouseListener { 035 /** 036 * Signifies that no action should be taken when the mouse is clicked in the 037 * DotPanel. 038 * 039 */ 040 public static final int NONE = 0; 041 /** Signifies that the ball position should be set when the mouse is clicked in the 042 * DotPanel. 043 */ 044 public static final int POS = 1; 045 /** 046 * Signifies that a gravity point should be added when the mouse is clicked in the 047 * DotPanel. 048 */ 049 public static final int GRAV = 2; 050 051 private Coord xCoord; 052 private Coord yCoord; 053 private DotPanel myPanel; 054 private int mode = SetPosGravListener.POS; 055 056 /** Creates a new SetPosGravListener that knows about a DotPanel and it's 057 * associated Coordinates. 058 * @param xCoord The x coordinate 059 * @param yCoord the y coordinate. 060 * @param myPanel The dot panel for which we will be a mouse listener. 061 */ 062 public SetPosGravListener(Coord xCoord, Coord yCoord, DotPanel myPanel){ 063 this.xCoord = xCoord; 064 this.yCoord = yCoord; 065 this.myPanel = myPanel; 066 } 067 068 // If the mouse is pressed do something depending on what mode you're in. 069 /** Perform our mode dependant behavior. In GRAV mode, add a gravity point. In 070 * POS mode adjust the position of the ball. In NONE mode do nothing. 071 * @param e The mouse event we are to react to 072 */ 073 public void mousePressed(MouseEvent e) { 074 double newX = e.getX()-myPanel.curWidth()/2; 075 double newY = myPanel.curHeight()/2-e.getY(); 076 077 if (mode == SetPosGravListener.POS) { 078 079 if ((!(myPanel.getCirc())) || 080 ((Math.pow(2*newX/myPanel.curWidth(),2) + 081 Math.pow(2*newY/myPanel.curHeight(),2)) < 1)) { 082 // Only let the user place inside the circle if in 083 // circular mode 084 085 System.out.println ("Ball moved to " + newX + ":" + newY); 086 087 xCoord.setPos(newX); 088 yCoord.setPos(newY); 089 } else { 090 System.out.println ("You can't place the ball there."); 091 } 092 } else if (mode == SetPosGravListener.GRAV) { 093 094 System.out.println ("Adding grav spot at: " + newX + ":" + newY); 095 096 myPanel.addGrav((int)newX, (int)newY); 097 } 098 099 myPanel.paintBuf(); 100 } 101 102 // Some methods that the interface requires. 103 /** Stub to fulfill MouseListener. 104 * @param e A MouseEvent 105 */ 106 public void mouseReleased(MouseEvent e) {} 107 /** Stub to fulfill MouseListener. 108 * @param e A MouseEvent 109 */ 110 public void mouseClicked(MouseEvent e) {} 111 /** Stub to fulfill MouseListener. 112 * @param e A MouseEvent 113 */ 114 public void mouseEntered(MouseEvent e) {} 115 /** Stub to fulfill MouseListener. 116 * @param e A MouseEvent 117 */ 118 public void mouseExited(MouseEvent e) {} 119 120 /** Change the mode of operation of this listener. Apropriate values are defined 121 * by the NONE POS and GRAV fields of this class. 122 * @param mode The new mode for this listener. 123 */ 124 public void setMode (int mode) { 125 this.mode = mode; 126 } 127 128 } 129 130 /* 131 * $Log: SetPosGravListener.java,v $ 132 * Revision 1.3 2003/01/17 19:31:08 gus 133 * Add Javadocs 134 * 135 * Revision 1.2 2003/01/15 17:36:10 gus 136 * adding log keywords to files that don't have them 137 * 138 */