001 /* 002 * ScribbleCanvas class 003 * $Id: ScribbleCanvas.java,v 1.1.1.1 2002/06/05 21:56:35 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 scribble; 014 015 import java.awt.*; 016 import java.awt.event.*; 017 import cs101.awt.Line; 018 019 /** 020 * An extended SmartCanvas that echos user's mouse strokes as lines. 021 * 022 * <P>Handles mouse events (by creating lines to put in the repository), 023 * can be given a Color in which to draw, can be cleared. 024 * 025 * <P>Copyright 1996 Massachusetts Institute of Technology 026 * 027 * @author Todd C. Parnell, tparnell@ai.mit.edu 028 * @author Maciej Stachowiak, maciej@ai.mit.edu 029 * @author Lynn Andrea Stein, las@ai.mit.edu 030 * @version $Id: ScribbleCanvas.java,v 1.1.1.1 2002/06/05 21:56:35 root Exp $ 031 * 032 * @see scribble.SmartCanvas 033 */ 034 public class ScribbleCanvas extends SmartCanvas { 035 036 // Some integers to store a point in 037 protected int currX, currY; 038 039 /** The current color to draw lines in. */ 040 protected Color currColor; 041 042 043 /** 044 * Creates a new ScribbleCanvas. 045 */ 046 public ScribbleCanvas() { 047 this(new ScribbleData()); 048 } 049 050 /** 051 * Creates a new ScribbleCanvas with associated ScribbleData. 052 */ 053 public ScribbleCanvas(ScribbleData sd) { 054 super(sd); 055 056 this.addMouseListener(new MouseListener() { 057 public void mouseClicked(MouseEvent e) {} 058 public void mouseEntered(MouseEvent e) {} 059 public void mouseExited(MouseEvent e) {} 060 public void mousePressed(MouseEvent e) { 061 setCoords(e.getX(), e.getY()); 062 } 063 public void mouseReleased(MouseEvent e) { 064 lineTo(e.getX(), e.getY()); 065 resetCoords(); 066 } 067 }); 068 069 this.addMouseMotionListener(new MouseMotionListener() { 070 public void mouseDragged(MouseEvent e) { 071 lineTo(e.getX(), e.getY()); 072 setCoords(e.getX(), e.getY()); 073 } 074 public void mouseMoved(MouseEvent e) {} 075 }); 076 077 this.resetCoords(); 078 } 079 080 /** 081 * Removes all lines from the data repository. 082 * 083 * Then forces a call to the paint method. 084 */ 085 public void clearLines() { 086 this.repository.clearLines(); 087 this.repaint(); 088 } 089 090 /** 091 * Changes the drawing color. 092 * @param c the new drawing color. 093 */ 094 public void setColor ( Color c ) { 095 this.currColor = c; 096 } 097 098 /** 099 * Gives the x and y coords impossible values to signal 100 * that new coords are expected. 101 */ 102 protected void resetCoords() { 103 this.currX = -1; 104 this.currY = -1; 105 } 106 107 /** 108 * Saves the starting point for drawing a line in currX, currY 109 * @param x the x coord of the starting point 110 * @param y the y coord of the starting point 111 */ 112 protected void setCoords( int x, int y ) { 113 this.currX = x; 114 this.currY = y; 115 } 116 117 /** 118 * Adds a line (from the saved start point to the point passed) 119 * to the data repository. 120 * @param endX the x coord of the line's endpoint 121 * @param endY the y coord of the line's endpoint 122 */ 123 protected void lineTo( int endX, int endY) { 124 this.repository.addLine(new Line(this.currX, this.currY, 125 endX, endY, this.currColor)); 126 } 127 } 128 129 130 /* Comments: 131 * 132 * History: 133 * $Log: ScribbleCanvas.java,v $ 134 * Revision 1.1.1.1 2002/06/05 21:56:35 root 135 * CS101 comes to Olin finally. 136 * 137 * Revision 1.2 2000/05/07 07:07:03 mharder 138 * Fixed javadoc @see tag. 139 * 140 * Revision 1.1 2000/05/06 22:30:58 mharder 141 * Moved to scribble subdirectory. 142 * 143 * Revision 1.8 1998/07/24 16:44:51 tparnell 144 * Placate new javadoc behavior 145 * 146 * Revision 1.7 1998/07/22 17:59:33 tparnell 147 * modifications to reflect new cs101.* package structure 148 * 149 * Revision 1.6 1998/07/21 19:06:38 tparnell 150 * added more javadoc 151 * 152 * Revision 1.5 1998/07/21 14:09:04 tparnell 153 * minor bugfixes necessary b/c 1.2 complier is more picky 154 * 155 * Revision 1.4 1998/07/20 18:55:32 tparnell 156 * Added javadoc and logging. Minor code mods for greater consistency 157 * between files. 158 * 159 * Revision 1.3 1998/06/04 19:01:11 tparnell 160 * minor mods 161 * 162 * Revision 1.2 1998/06/04 16:59:57 tparnell 163 * old version was incomplete. this version is an import from fall96 164 * with updates to Java 1.1 and corrrections to work with new files 165 * 166 * Revision 1.6 1996/10/28 18:06:47 nathanw 167 * Revert the change of putting them in a Scribble package. 168 * 169 * Revision 1.5 1996/10/26 21:04:17 nathanw 170 * Put all this stuff into a Scribble package 171 * 172 * Revision 1.4 1996/10/24 22:43:05 las 173 * Changed everything. Well, almost: 174 * 175 * Both DumbDemo.java and WhiteboardDemo.java are standalone 176 * applications. DumbDemo pops up the window w/all the appropriate 177 * widgets, but nothing much happens. WhiteboardDemo is the fully 178 * functioning solution version. 179 * 180 * Things have been broken up into what they write and what we give 181 * them. They get ScribbleData, SmartCanvas, and Whiteboard. They write 182 * DumbDemo and ScribbleCanvas, and (if they're ambitious) maybe 183 * WhiteboardSoln/Demo. They could even extend WhiteboardSoln further, 184 * putting in extra components, but then they'd have to significantly 185 * extend or at least override Whiteboard as well. 186 * 187 * ScribbleData remained roughly the same. 188 * ScribbleCanvas got broken into SmartCanvas (handles the repository & 189 * painting) and ScribbleCanvas (handles the mouse events & optionally 190 * the clear/color methods). 191 * Whiteboard got broken into Whiteboard (sets up the widgets, doesn't do 192 * anything with them and WhiteboardSoln (handles the action events). 193 * Also, the Applet's init was retained, since it does the Right Thing. 194 * There is some hair in (1) how constructor arguments are handled and 195 * (2) the (non-)overriding of drawingArea, but all appears to work 196 * smoothly. I don't expect students to come up with this particular 197 * solution. 198 * WhiteboardDemo and DumbDemo got added, as they turn things into a 199 * standalone. 200 * 201 * Revision 1.3 1996/08/04 05:37:26 reuben 202 * Changed setColor and setData methods to be protected. 203 * This allows access from non-subclass objects in the same package. 204 * 205 * Revision 1.2 1996/08/03 23:06:16 reuben 206 * Tweaked comments. 207 * 208 * Revision 1.1.1.1 1996/07/23 17:21:27 sit 209 * Import from summer 6.80s web tree 210 * 211 * Revision 1.1 1996/07/05 15:15:26 las 212 * Initial revision 213 * 214 * 215 */ 216