001 /* 002 * cs101 DisplayField utility 003 * $Id: DisplayField.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.DisplayField extends cs101.util.ColorField to add a line of 019 * text to the colored rectangle.<p> The size of the rectangle is supposed 020 * to change with the length of the text. The color of the field can also 021 * change between two predetermined colors. 022 * 023 * <P>Copyright 1996 Massachusetts Institute of Technology 024 * 025 * @author Todd C. Parnell, tparnell@ai.mit.edu 026 * @author Joshua R. Brown, reuben@ai.mit.edu 027 * @version $Id: DisplayField.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $ 028 */ 029 public class DisplayField extends ColorField { 030 031 /** the string to be displayed */ 032 protected String text; 033 034 // DisplayField(String, boolean, Dimension, Color, Color) 035 /** 036 * Constructs a rectangular colorfield with the passed 037 * attributes. 038 * 039 * @param text The text to intially display in the field. 040 * @param initState The initial state of the color field. 041 * @param trueColor The color of the field when the state is true. 042 * @param falseColor The color of the field when the state is false. 043 */ 044 public DisplayField(String text, boolean initState, Color trueColor, 045 Color falseColor) { 046 super(initState, new Dimension(25,25), trueColor, falseColor); 047 this.text = text; 048 } 049 050 // setText(String) 051 /** 052 * Changes the text of the DisplayField to the string passed. 053 * 054 * Should also resize the component to fit the text. 055 * This is not fully implemented yet. 056 * 057 * @param text The string to be displayed in the field. 058 */ 059 public void setText(String text) { 060 this.text = text; 061 measure(); 062 repaint(); 063 } 064 065 /** 066 * This method is called to determine the size of the 067 * rectangle given the current text to display. 068 */ 069 private void measure() { 070 Dimension oldSize = new Dimension(this.dim); 071 072 FontMetrics fm = this.getFontMetrics(this.getFont()); 073 074 // If we don't have font metrics yet go with our guess 075 if (fm == null) 076 return; 077 078 this.dim.height=fm.getHeight()+16; 079 this.dim.width=fm.stringWidth(this.text)+20; 080 081 } 082 083 /** 084 * Calls super.addNotify. Then measures it's intial size. 085 * <br> 086 * Note: Must be called from here so that the Font Metrics is available 087 * for measuring. 088 */ 089 public void addNotify() { 090 super.addNotify(); 091 measure(); 092 } 093 094 /** 095 * Called by this object's container 096 * to determine the minimum space required by this object. 097 * 098 * @return the prefered size of the object 099 */ 100 public Dimension getPreferredSize() { return this.dim; } 101 102 /** 103 * Called by this object's container 104 * to determine the minimum space required by this object. 105 * 106 * @return the minimum size of the object 107 */ 108 public Dimension getMinimumSize() { return this.dim; } 109 110 /** 111 * Calls the ColorField.paint to do the field. 112 * Then adds the text in black. 113 * 114 * @param g The graphics context to paint into. 115 */ 116 public void paint(Graphics g) { 117 super.paint(g); 118 119 // draw the text 120 g.setColor(Color.black); 121 g.drawString(this.text,10,this.dim.height-10); 122 123 } 124 125 } 126 127 128 /* Comments: 129 * 130 * History: 131 * $Log: DisplayField.java,v $ 132 * Revision 1.1.1.1 2002/06/05 21:56:32 root 133 * CS101 comes to Olin finally. 134 * 135 * Revision 1.4 1998/07/24 17:06:28 tparnell 136 * Placate new javadoc behavior 137 * 138 * Revision 1.3 1998/07/22 18:18:36 tparnell 139 * migration from cs101.util to cs101.* 140 * 141 * Revision 1.2 1998/06/03 19:32:20 tparnell 142 * update from Java 1.0 to 1.1 143 * 144 * Revision 1.1 1998/03/13 22:18:12 tparnell 145 * Import from server crash. I think the src and class files match up. 146 * 147 * Revision 1.4 1996/08/01 18:26:20 reuben 148 * More javadoc tweaking (hopefully the final pass) 149 * 150 * Revision 1.3 1996/08/01 16:19:55 reuben 151 * Fixed javadoc problem with return. 152 * 153 * Revision 1.2 1996/07/25 18:27:41 reuben 154 * Added all kinds of comments. 155 * Compiled and tested. 156 * 157 */