001    /*
002     * SetVListener.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    /** 
019     * This class listens to the set velocity button in the AdvEnv
020     * Frame and sets the velocity of the dot with whatever text is in
021     * the TextArea.
022     *
023     * <p>Copyright © 1998 Massachusetts Institute of Technology<br />
024     * Copyright © 2003 Franklin W. Olin College of Engineering</p>
025     *
026     * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu
027     * @author Henry Wong, henryw@mit.edu
028     * @author Patrick G. Heck, gus.heck@olin.edu
029     * @version $Id: SetVListener.java,v 1.4 2004/02/09 20:55:03 gus Exp $
030     * @see Coord
031     * @see DotPanel
032     */
033    public class SetVListener implements ActionListener {
034      private TextField xVel;
035      private TextField yVel;
036      private Coord xCoord;
037      private Coord yCoord;
038        
039      /** Creates a new SetVListener to listen to the velocity setter button in AdvEnv.
040       * @param xVel The x velocity
041       * @param yVel The y velocity
042       * @param xCoord The coordiante along the X axis
043       * @param yCoord The coordiante along the Y axis
044       */  
045      public SetVListener(TextField xVel, TextField yVel, Coord xCoord, Coord yCoord) {
046        this.xVel = xVel;
047        this.yVel = yVel;
048        this.xCoord = xCoord;
049        this.yCoord = yCoord;
050      }
051        
052      /** Respond to the button and set the Velocity.
053       * @param ae The Event from the set velocity button
054       */  
055      public void actionPerformed(ActionEvent ae) {
056        double newX;
057        double newY;
058    
059        try {
060          newX = Double.valueOf(xVel.getText()).doubleValue();
061          newY = Double.valueOf(yVel.getText()).doubleValue();
062        } catch (NumberFormatException e) {
063          System.out.println ("Error setting velocity");
064          return;
065        }   
066            
067        // There is a maximum velocity that I set, b/c otherwise if the
068        // ball is moving too fast things start to get really messy and
069        // inaccurate.
070            
071        if (Math.sqrt(Math.pow(newX,2) + Math.pow(newY,2)) >
072            Spirograph.MAXVEL) {
073          newX = Spirograph.MAXVEL*newX/(newX+newY);
074          newY = Spirograph.MAXVEL*newY/(newX+newY);
075          xCoord.setVel(newX);
076          yCoord.setVel(newY);
077          xVel.setText(String.valueOf(Math.round(newX)));
078          yVel.setText(String.valueOf(Math.round(newY)));
079                
080          System.out.println ("Velocity too high, being coerced down.");
081        }
082            
083        xCoord.setVel(newX);
084        yCoord.setVel(newY);
085        System.out.println ("Velocity set");
086    
087      }
088    }
089    
090    /*
091     * $Log: SetVListener.java,v $
092     * Revision 1.4  2004/02/09 20:55:03  gus
093     * javadoc fixes
094     *
095     * Revision 1.3  2003/01/17 20:49:07  gus
096     * Added Javadoc
097     *
098     * Revision 1.2  2003/01/15 17:36:10  gus
099     * adding log keywords to files that don't have them
100     *
101     */