1  // File:  DrawCanvas.java
  2  
  3  import java.awt.*;
  4  import java.awt.event.*;
  5  import java.applet.*;
  6  
  7  public class DrawCanvas 
  8  		extends Canvas 
  9  {
 10  
 11  	Graphics g;
 12  	String curMode="Rect";
 13  	mPoint curObj;
 14  	public boolean fill=true;
 15  	mPoint objList[]=new mPoint[100];
 16  	int index=0;
 17  
 18  
 19  	public DrawCanvas() {
 20  		
 21  		setBackground(Color.blue);
 22  		setForeground(Color.yellow);
 23  		
 24  		// Notice the construction of two listener objects, ourMouseListener
 25  		// and ourMotionListener. Both are constructed from the class 
 26  		// definitions, called anonymous classes.
 27  
 28  		// "You should consider using an anonymous class instead of
 29  		// [a top level class or] a local class if:
 30  		// . The class has a very short body.
 31  		// . Only one instance of the class is needed.
 32  		// . The class is used right after it is defined.
 33  		// . The name of the class does not make your code any easier to understand."
 34  		// [Java in a Nutshell, pg. 119]
 35  		
 36  		// We define our version of mouse adapters here 
 37  		// because we will not use all of the MouseListener methods.
 38  
 39  		// MouseListener is an interface and all of its methods are defined abstract.
 40  		// This forces us to overwrite all the methods even though we do not need some.
 41  		// To overcome this problem, adapter classes are developed. These are 
 42  		// classes implementing interfaces with empty method bodies. Hence, we only
 43  		// overwrite those methods that we need. 
 44  
 45  		// In this case, we need mousePressed and mouseReleased.
 46  		MouseListener ourMouseListener = new MouseAdapter() {
 47  			// from MouseAdapter
 48  			public void mousePressed(MouseEvent e) {
 49  				int x=e.getX();
 50  				int y=e.getY();
 51  				if (curMode.equals("Rect")) curObj=new mRectangle(x, y, x, y);
 52  				else if (curMode.equals("Circ")) curObj=new mCircle(x, y, x, y);
 53  				else if (curMode.equals("Line")) curObj=new mLine(x, y, x+1, y+1);
 54  				else if (curMode.equals("Round")) curObj=new mRound(x, y, x, y);
 55  			}
 56  		
 57  			// from MouseAdapter
 58  			public void mouseReleased(MouseEvent e) {
 59  				g=getGraphics();
 60  				g.setPaintMode();
 61  				curObj.setEnds(e.getX(), e.getY());
 62  				curObj.setFill(fill);
 63  				objList[index++]=curObj;
 64  				curObj.draw(g);
 65  				if(index>=100) index=100;
 66  			}
 67  		};
 68  		
 69  		// We also need mouseDragged from MouseMotionListener.
 70  		MouseMotionListener ourMotionListener = new MouseMotionAdapter() {
 71  			public void mouseDragged(MouseEvent e) {
 72  				g=getGraphics();
 73  				g.setXORMode(Color.black);
 74  				curObj.draw(g);
 75  				curObj.setEnds(e.getX(), e.getY());
 76  				curObj.draw(g);
 77  			}
 78  		};
 79  		
 80  		addMouseListener( ourMouseListener );
 81  		addMouseMotionListener( ourMotionListener );
 82  	}
 83  
 84  	public void setDrawMode(String s) {
 85  		if(s.equals("Clear")) {
 86  			index=0;
 87  			repaint();
 88  		} 
 89  		else if(s.equals("Redraw")) repaint();
 90  		else curMode=s;
 91  	}
 92  
 93  	public void paint(Graphics g) {
 94  
 95  		g.setColor(Color.yellow);
 96  		super.paint(g);
 97  		for(int i=0; i<index; i++)
 98  			objList[i].draw(g);
 99  	}
100  }
101  
102  class mPoint {
103  
104  	
105  	int x,y,x2,y2;
106  	boolean fill=false;
107  
108  	public mPoint(int _x, int _y, int _x2, int _y2) {
109  
110  		x=_x;
111  		y=_y;
112  		x2=_x2;
113  		y2=_y2;
114  	};
115  
116  	public void draw(Graphics g) {
117  	}
118  
119  	public void setFill(boolean _fill) {
120  		fill=_fill;
121  	}
122  
123  	public boolean isIn(int _x, int _y) {
124  		return _x == x && _y == y;
125  	}
126  
127  	public void setEnds(int _x, int _y) {
128  		x2=_x;
129  		y2=_y;
130  	}
131  }
132  
133  class mLine extends mPoint {
134  
135  	public mLine(int _x, int _y, int _x2, int _y2) {
136  		super(_x, _y, _x2, _y2);
137  	};
138  
139  	public void draw(Graphics g) {
140  		g.drawLine(x, y, x2, y2);
141  	}
142  }
143  
144  class mRectangle extends mPoint {
145  
146  	public mRectangle(int _x, int _y, int _x2, int _y2) {
147  		super(_x, _y, _x2, _y2);
148  	};
149  
150  	public void draw(Graphics g) {
151  		if (fill) {
152  			g.setColor(Color.black);
153  			g.fillRect(x, y, x2-x, y2-y);
154  			g.setColor(Color.yellow);
155  		}
156  		g.drawRect(x, y, x2-x, y2-y);
157  	}
158  
159  	public boolean isIn(int _x, int _y) {
160  		Rectangle r=new Rectangle(x, y, x2-x, y2-y);
161  		return r.inside(_x, _y);
162  	}
163  }
164  
165  class mRound extends mRectangle {
166  
167  	public mRound(int _x, int _y, int _x2, int _y2) {
168  		super(_x, _y, _x2, _y2);
169  	};
170  
171  	public void draw(Graphics g) {
172  		if (fill) {
173  			g.setColor(Color.black);
174  			g.fillRoundRect(x, y, x2-x, y2-y, 100, 50);
175  			g.setColor(Color.yellow);
176  		}
177  		g.drawRoundRect(x, y, x2-x, y2-y, 100, 50);
178  	}
179  }
180  
181  class mCircle extends mRectangle {
182  
183  	public mCircle(int _x, int _y, int _x2, int _y2) {
184  		super(_x, _y, _x2, _y2);
185  	};
186  
187  	public void draw(Graphics g) {
188  		if (fill) {
189  			g.setColor(Color.black);
190  			g.fillOval(x, y, x2-x, y2-y);			
191  			g.setColor(Color.yellow);
192  		}
193  		g.drawOval(x, y, x2-x, y2-y);
194  	}
195  }
196  
197