001 /*
002 * cs101 Line utility
003 * $Id: Line.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.Color;
016 import java.awt.Graphics;
017 import java.util.StringTokenizer;
018
019 /**
020 * Implements a 4-coordinate (+ optional Color) line abstraction with
021 * its own drawing method. Also, implements a translation between
022 * Line objects and String objects for handling by less intelligent
023 * protocols.
024 *
025 * <P>Copyright 1996 Massachusetts Institute of Technology
026 *
027 * @author Maciej Stachowiak, maciej@ai.mit.edu
028 * @author Lynn Andrea Stein, las@ai.mit.edu
029 * @version $Id: Line.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
030 *
031 * @see java.awt.Graphics
032 */
033 public class Line {
034 private Color c;
035 private int startX, startY, endX, endY;
036
037 /**
038 * This specifies the default color for a line.
039 * It may not be changed.
040 */
041 public static final Color DEFAULT_COLOR = Color.black;
042
043 /**
044 * Construct a line, specifying all 4 coordinates and using
045 * the default color.
046 */
047 public Line ( int startX, int startY, int endX, int endY ) {
048 this( startX, startY, endX, endY, Line.DEFAULT_COLOR );
049 }
050
051 /**
052 * Construct a line, specifying all 4 coordinates and a specific
053 * color.
054 *
055 * @see java.awt.Color
056 */
057 public Line ( int startX, int startY, int endX, int endY, Color c ) {
058 this.startX = startX;
059 this.startY = startY;
060 this.endX = endX;
061 this.endY = endY;
062 if (c == null) this.c = Line.DEFAULT_COLOR;
063 else /* c != null */ this.c = c;
064 }
065
066 public void drawOn (Graphics g) {
067 g.setColor(c);
068 g.drawLine(this.startX, this.startY, this.endX, this.endY);
069 }
070
071 // The rest of this class is for use with primitive datahandlers
072 // which can only cope with Strings. It provides a utility package
073 // which enables coercion between a particular String representation
074 // and objects of the Line class.
075 private Line() {}
076
077 /**
078 * Given a String that was originally created by packLine, this
079 * function will return a reference to a new Line object that
080 * represents the original Line.
081 *
082 * @see #packLine
083 */
084 public static Line extractLine( String s ) {
085 StringTokenizer tokens = new StringTokenizer(s," \t\r\n:");
086 Line l = new Line();
087 l.startX = Integer.parseInt( tokens.nextToken() );
088 l.startY = Integer.parseInt( tokens.nextToken() );
089 l.endX = Integer.parseInt( tokens.nextToken() );
090 l.endY = Integer.parseInt( tokens.nextToken() );
091 if ( tokens.hasMoreTokens() ) {
092 l.c = new Color( Integer.parseInt( tokens.nextToken() ) );
093 } else {
094 l.c = Line.DEFAULT_COLOR;
095 }
096 return l;
097 }
098
099 /**
100 * This method converts a Line into a String.
101 *
102 * @see #extractLine
103 */
104 public static String packLine( Line l ) {
105 return ( Integer.toString( l.startX )
106 + ":"
107 + Integer.toString( l.startY )
108 + ":"
109 + Integer.toString( l.endX )
110 + ":"
111 + Integer.toString( l.endY )
112 + ":"
113 + Integer.toString( l.c.getRGB() )
114 + "\n" );
115 }
116 }
117
118
119 /* Comments:
120 *
121 * History:
122 * $Log: Line.java,v $
123 * Revision 1.1.1.1 2002/06/05 21:56:32 root
124 * CS101 comes to Olin finally.
125 *
126 * Revision 1.4 1998/07/24 17:06:30 tparnell
127 * Placate new javadoc behavior
128 *
129 * Revision 1.3 1998/07/22 18:18:39 tparnell
130 * migration from cs101.util to cs101.*
131 *
132 * Revision 1.2 1998/06/04 18:53:58 tparnell
133 * prevented passing in color = null from raising exception
134 *
135 * Revision 1.1 1998/03/13 22:18:15 tparnell
136 * Import from server crash. I think the src and class files match up.
137 *
138 * Revision 1.4 1996/11/18 17:25:03 las
139 * Added revised SharedWhiteboard support classes. These versions of
140 * Client and Server supercede the previous ones and are not directly
141 * backwards compatible. In particular, Server is an instantiable class
142 * rather than a primarily static one (use RunServer to run it), and
143 * Client uses StringHandler rather than subclassing to specialize it.
144 * Line.java just picked up some obscure documentation along the way.
145 * Otherwise, classes are direct imports from SharedWhiteboard.
146 *
147 * Revision 1.1 1996/11/18 17:10:15 las
148 * All files appear to be working. Some of the files in this directory
149 * belong in cs101.util. These will be moved Real Soon Now. Also, added
150 * ClientMonitor, which watches net traffic but doesn't send.
151 *
152 * Revision 1.3 1996/08/01 18:26:29 reuben
153 * More javadoc tweaking (hopefully the final pass)
154 *
155 * Revision 1.2 1996/07/30 17:26:00 reuben
156 * Added/corrected javadoc comments.
157 *
158 * Revision 1.1.1.1 1996/07/18 17:38:24 sit
159 * Import from /mit/6.096/share/classes after 6.80s session
160 *
161 * Revision 1.3 1996/07/11 14:48:06 sit
162 * Added documentation.
163 *
164 * Revision 1.2 1996/07/02 22:33:17 las
165 * Fixed package....
166 *
167 * Revision 1.1 1996/07/02 21:47:51 las
168 * Initial revision
169 *
170 */
171