1  import java.awt.*;

  2  import java.applet.*;

  3  

  4  public class DrawCanvas extends Canvas {

  5  

  6  	Graphics g;

  7  	String curMode="Rect";

  8  	mPoint curObj;

  9  	public boolean fill=true;

 10  	mPoint objList[]=new mPoint[100];

 11  	int index=0;

 12  

 13  

 14  	public DrawCanvas() {

 15  		

 16  		super();

 17  		setBackground(Color.blue);

 18  		setForeground(Color.yellow);

 19  	} 

 20  

 21  	public void setDrawMode(String s) {

 22  		if(s.equals("Clear")) {

 23  			index=0;

 24  			repaint();

 25  		} else if(s.equals("Redraw")) repaint();

 26  		else curMode=s;

 27  	}

 28  

 29  	public void paint(Graphics g) {

 30  

 31  		g.setColor(Color.yellow);

 32  		super.paint(g);

 33  		for(int i=0; i<index; i++)

 34  			objList[i].draw(g);

 35  	}

 36  

 37  	public boolean mouseDown(Event e, int x, int y) {		

 38  		

 39  		if (curMode.equals("Rect")) curObj=new mRectangle(x, y, x, y);

 40  		else if (curMode.equals("Circ")) curObj=new mCircle(x, y, x, y);

 41  		else if (curMode.equals("Line")) curObj=new mLine(x, y, x+1, y+1);

 42  		else if (curMode.equals("Round")) curObj=new mRound(x, y, x, y);

 43  		return true;

 44  	}

 45  

 46  	public boolean mouseDrag(Event e, int x, int y) {

 47  

 48  		g=getGraphics();

 49  		g.setXORMode(Color.black);

 50  		curObj.draw(g);

 51  		curObj.setEnds(x, y);

 52  		curObj.draw(g);

 53  

 54  		return true;

 55  	}

 56  

 57  	public boolean mouseUp(Event e, int x, int y) {		

 58  

 59  		g=getGraphics();

 60  		g.setPaintMode();

 61  		curObj.setEnds(x, y);

 62  		curObj.setFill(fill);

 63  		objList[index++]=curObj;

 64  		curObj.draw(g);

 65  		if(index>=100) index=100;

 66  		

 67  		return true;

 68  	}

 69  }