001    /*
002     * cs101 ColorField utility 
003     * $Id: ColorField.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
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 cs101.awt;
014    
015    import java.awt.*;
016    
017    /**
018     * cs101.awt.ColorField provides a colored rectangle that operates
019     * as an awt.component.<p> 
020     *
021     * The color field is setup to have two different states represented
022     * by two different colors.  The field can be of any size or color.
023     *
024     * <P>Copyright (c) 1998 Massachusetts Institute of Technolgoy
025     *
026     * @author   Todd C. Parnell, tparnell@ai.mit.edu
027     * @author   Joshua R. Brown, reuben@ai.mit.edu
028     * @version  $Id: ColorField.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
029     */
030    public class ColorField extends Canvas {
031    
032      /** determines the color of the field */
033      protected boolean state;
034      /** size of the field */
035      protected Dimension dim;
036      /** color when state == true */
037      protected Color trueColor;
038      /** color when state == false */
039      protected Color falseColor;
040    
041      // ColorField()
042      /**
043       * Constructs a rectangular color field with default values
044       * for all attibutes.
045       * <br>
046       * o  Initial state = false <br>
047       * o  Dimension = 25 X 25  <br>
048       * o  Color when state is true = green <br>
049       * o  Color when state is false = red <br>
050       */
051       public ColorField() {
052         this(false, new Dimension(25,25), Color.green, Color.red);
053       }
054    
055      // ColorField(boolean, Dimension, Color, Color)
056      /**
057       * Constructs a rectangular colorfield with the passed
058       * attributes.
059       *
060       * @param initState  The initial state of the color field.
061       * @param dim        The dimensions of the color field.
062       * @param trueColor  The color of the field when the state is true.
063       * @param falseColor The color of the field when the state is false.  
064       */
065       public ColorField(boolean initState, Dimension dim,
066                         Color trueColor, Color falseColor) {
067         this.state = initState;
068         this.dim = dim;
069         this.trueColor = trueColor;
070         this.falseColor = falseColor;
071       }
072    
073      // changeState(boolean)
074      /**
075       * Changes the state of the color field to the value passed in.
076       *
077       * Changing the state of the field will cause the field to repainted
078       * in the appropriate color.  If the state is unchanged nothing happens.
079       *
080       * @param newState  The new state of the color field.
081       */
082      public void changeState(boolean newState) {
083        if (state != newState) {
084          this.state = newState;
085          this.repaint();
086        }
087      }
088    
089      /**
090       * Draws the color field on the screen in it's current state
091       * 
092       * @param g The graphics context to paint into.
093       */
094      public void paint(Graphics g) {
095        // draw the outline
096        g.setColor(Color.black);
097        g.drawRect(0,0,this.dim.width-1,this.dim.height-1);
098    
099        // draw the field
100        if (this.state) 
101          g.setColor(this.trueColor);
102        else 
103          g.setColor(this.falseColor);
104        g.fillRect(2,2,this.dim.width-3,this.dim.height-3);
105    
106      }
107    
108      /**
109       * Called by this object's container
110       * to determine the minimum space required by this object. 
111       *
112       * @return the minimum size of the object
113       */
114      public Dimension getMinimumSize() {
115         return new Dimension(this.dim.width,this.dim.height);
116      }
117    
118      /**
119       * Called by this object's container
120       * to determine the minimum space required by this object. 
121       *
122       * @return  the prefered size of the object
123       */   
124      public Dimension getPreferredSize() {
125        return new Dimension(this.dim.width,this.dim.height);
126      }
127      
128    }
129    
130    /* Comments:
131     *
132     * History:
133     *     $Log: ColorField.java,v $
134     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
135     *     CS101 comes to Olin finally.
136     *
137     *     Revision 1.4  1998/07/24 17:06:27  tparnell
138     *     Placate new javadoc behavior
139     *
140     *     Revision 1.3  1998/07/22 18:18:34  tparnell
141     *     migration from cs101.util to cs101.*
142     *
143     *     Revision 1.2  1998/06/03 18:50:49  tparnell
144     *     migration from Java 1.0 to Java 1.1.  used deprecated APIs
145     *
146     *     Revision 1.1  1998/03/13 22:18:10  tparnell
147     *     Import from server crash.  I think the src and class files match up.
148     *
149     *     Revision 1.4  1996/08/01 18:26:19  reuben
150     *     More javadoc tweaking (hopefully the final pass)
151     *
152     *     Revision 1.3  1996/08/01 16:18:21  reuben
153     *     Fixed javadoc problem with return
154     *
155     *     Revision 1.2  1996/07/25 18:27:41  reuben
156     *     Added all kinds of comments.
157     *     Compiled and tested.
158     *
159     */
160    
161    
162    
163    
164    
165    
166    
167