import java.applet.*; import java.awt.*; /** * This class implements a drawing board. * It captures the users mouseDown and mouseUp * events setting the drawing boundaries, and * draws a shape according to the user's * specifications obtained from the controlStrip * object */ public class whiteBoard extends Panel { // These variables store the x and y coordinates // obtained upon the mouseDown and mouseUp events // generated by the user protected int startX=0, prevX=0, currX=0; protected int startY=0, prevY=0, currY=0; // These variables store the actual dimensions // of the area that holds the shape to be drawn protected int width=0,prevWidth = 0, height=0,prevHeight=0; // These variables help determine the direction the user is // dragging the cursor before releasing the mouse button protected boolean widthIsNeg = false, heightIsNeg = false; protected String drawShape = "Line", drawColor = "Green", fillColor = ""; // initializes the class public void whiteBoard () { setBackground(Color.lightGray); //sets background to light gray setForeground(Color.green); //and drawing color to green } /*---------------------------------------------*/ /** * This function handles the mouse down event * It registers coordinates of the mouse click * within the applet space * It also sets the shapes color according to * the user's choice */ /*---------------------------------------------*/ public boolean mouseDown(Event e, int x, int y) { // initialize variables upon mouseDown event startX = x; startY = y; currX = x; currY = y; prevX=0; prevY=0; prevWidth=0; prevHeight=0; // Check for the user's color choice // and set the foreground color accrodingly if (drawColor == "Green") setForeground(Color.green); else if (drawColor == "Blue") setForeground(Color.blue); else setForeground(Color.yellow); return(true); } /*-----------------------------------------------*/ /** * This function handles the mouseUp event * It registers coordinates of the mouse release * within the applet space * It also checks for what type of shape the user * chose, and draws it */ /*-----------------------------------------------*/ public boolean mouseUp(Event e, int x, int y) { // if it's a line, draw it // (currX and currY hold the coordinates // from the mouseDown event if (drawShape == "Line") getGraphics().drawLine(currX,currY,x,y); else { // It's either an ellipse or a rectangle // determine the shape's dimensions width = x - startX; height = y - startY; // if user is dragging from right to left if (width < 0){ width = width * (-1); // make the width a positive value currX = x; // currX holds the release x coordinate widthIsNeg = true; // set the flag to true } // if user is dragging from bottom to top if (height < 0){ height= height * (-1); // make the height a positive value currY = y; // currY holds the release y coordinate heightIsNeg = true; // set the flag to true } // if shape is a rectangle if (drawShape == "Rectangle"){ // Determine fill style if (fillColor == "Hollow") // draws a hollow rectangle getGraphics().drawRect(currX,currY,width,height); else // draws a filled rectangle getGraphics().fillRect(currX,currY,width,height); } // if shape is an ellipse else if (drawShape == "Ellipse"){ // Detemine fill style if (fillColor == "Hollow") // draws a hollow ellipse getGraphics().drawOval(currX,currY,width,height); else // draws a filled ellipse getGraphics().fillOval(currX,currY,width,height); } } return(true); } /*------------------------------*/ /** * This function is called upon * changing the shape choice in * the control strip */ /*------------------------------*/ public void changeShape(String shape){ drawShape = shape; // shape: user's choice of shape } /*------------------------------*/ /** * This function is called upon * changing the color choice in * the control strip */ /*------------------------------*/ public void changeColor(String color){ drawColor = color; // color: user's choice of color } /*------------------------------*/ /** * This function is called upon * changing the style choice in * the control strip */ /*------------------------------*/ public void changeFillColor(String color){ fillColor = color; // color: user's choice of style (hollow or filled) } /*------------------------------*/ /** * This function is called upon * pushing the reset button */ /*------------------------------*/ public void clearDrawingArea (){ getGraphics().clearRect(0,0,500,500); } }