import java.awt.*; import java.applet.Applet; public class Asst7 extends Applet { DrawingCanvas canvas; public void init() { GridLayout grid = new GridLayout(1,0); setLayout(grid); add (new DrawingCanvas()); validate(); } } class DrawingCanvas extends Canvas { // Shapes private static final int MAX_SHAPES=5; private static final int OVAL=0; private static final int SOLID_OVAL=1; private static final int RECTANGLE=2; private static final int SOLID_RECT=3; private static final int LINE=4; // Colors private static final int MAX_COLORS=4; private static final int RED=0; private static final int BLUE=1; private static final int GREEN=2; private static final int YELLOW=3; // States private static final int DRAWING=0; private static final int CHANGING_POSITION=1; // private Dimension shapeSize; private int pressedX; private int pressedY; private int color=BLUE; private int shape=OVAL; private int state=CHANGING_POSITION; //used for rubber shapes private int cursorX; private int cursorY; // constructor public DrawingCanvas() { super(); pressedX = pressedY =-1; cursorX=cursorY=-1; } // called when mouse button is pressed // (x,y) is position of mouse press // ignore the event parameter public boolean mouseDown(Event event, int x, int y) { // (for debugging purposes?) System.out.println ("mouse pressed at (" + x + ", " + y + ")"); if (y<20 && x<40) { if (x <40) { if (x<20){ shape=(shape+1)%MAX_SHAPES; drawMenu(); System.out.println ("Changing shape to " + shape); } else { color=(color+1)%MAX_COLORS; drawMenu(); System.out.println ("Changing color to " + color); } } return false; } pressedX = cursorX= x; pressedY = cursorY= y; return false; } // called when mouse is moved without holding down any button public boolean mouseMove(Event event, int x, int y) { drawMenu(); return false; } // called when mouse is moved while holding down the button public boolean mouseDrag(Event event, int x, int y) { if ((x>=40) || (y>=20)) { drawRubber(cursorX, cursorY); drawRubber(x,y); cursorX=x; cursorY=y; state=DRAWING; } return false; } // called when mouse button is released public boolean mouseUp(Event event, int x, int y) { if (state==DRAWING) { drawRubber(cursorX, cursorY); /* delete the last rubber */ Dimension d= new Dimension(x-pressedX, y-pressedY); Graphics g =getGraphics(); drawShape(g, pressedX, pressedY, color, shape, d); state=CHANGING_POSITION; g.dispose(); } return false; } public void drawShape(Graphics g, int x, int y, int color, int shape, Dimension d) { setColor(g, color); int ulX=d.width>0 ? x : x+d.width; int ulY=d.height>0 ? y : y+d.height; int w=d.width<0 ? -d.width:d.width; int h=d.height<0 ? -d.height:d.height; switch (shape) { case OVAL: g.drawOval(ulX,ulY,w,h); break; case SOLID_OVAL: g.fillOval(ulX,ulY,w,h); break; case RECTANGLE: g.drawRect(ulX,ulY,w,h); break; case SOLID_RECT: g.fillRect(ulX,ulY,w,h); break; case LINE: g.drawLine(x, y , x+d.width, y+d.height); break; } } public void setColor(Graphics g, int color) { switch(color) { case RED: g.setColor(Color.red); break; case GREEN: g.setColor(Color.green); break; case BLUE: g.setColor(Color.blue); break; case YELLOW: g.setColor(Color.yellow); break; } } public void drawRubber(int x, int y) { Dimension d= new Dimension(x-pressedX, y-pressedY); drawXOR(pressedX, pressedY, color, shape, d); } public void drawMenu() { Dimension d = new Dimension(20,20); Graphics g = getGraphics(); g.clearRect(0,0,40,20); Dimension shape_d = new Dimension(15,15); drawShape(g,3,3, color,shape,shape_d); drawShape(g,0,0,BLUE,RECTANGLE,d); drawShape(g,20,0,color,SOLID_RECT,d); drawShape(g,20,0,BLUE,RECTANGLE,d); g.dispose(); } public void drawXOR(int x, int y, int color, int shape, Dimension d) { Graphics g = getGraphics(); g.setXORMode(getBackground()); drawShape(g,x,y,color,shape, d); g.dispose(); } }