Lab 14b Mouse Motion Listeners etc.

Part 1
In your DrawPad method, import the package quadrilaterals. Declare a private ArrayList named shapeList. Add either a combo box or a menu to select a particular Quadrilateral. Once selected when the user clicks and drags on the bottom panel capture the x1, y1 coordinates of the event in the mouse pressed method:

public void mousePressed(MouseEvent e) {
   if(e.getButton() == 1 && !drawRect )
   {
      x1= e.getX();
      y1= e.getY();
   }
}
When the user releases the mouse with the mouse's button == 1, capture x2, y2 coordinates of the event. Use these four coordinates to instantiate the appropriate Quadrilateral, depending on the case selected:
case KITE:    if (y1 >= y2) shape = new Kite(x2,y2,x1,y1);
                     else shape = new Kite(x1,y1,x2,y2);
                     System.out.println(shape);
break;
case TRAPEZOID: shape = new Trapezoid(x1,y1,x2,y2);
                    System.out.println(shape);
break;
case ISOTRAP: shape = new IsocelesTrapezoid(x1,y1,x2,y2);
                    System.out.println(shape);
break;
case PARALLELOGRAM: shape = new Parallelogram(x1,y1,x2-x1,y2-y1, 15);
                    System.out.println(shape);
break;
case RHOMBUS: shape = new Rhombus(x1,y1,x2,y2);
                    System.out.println(shape);
break;
case RECTANGLE: shape = new Rectangle(x1,y1,x2-x1,y2-y1);
                    System.out.println(shape);
break;
case SQUARE: shape = new Square(x1,y1,x2-x1);
                    System.out.println(shape);
break;
Add the newly instantiated shape to your shapeList. Draw your shape using g.fillPolygon(shape).
When the Rectangle button is clicked, the Boolean field drawRect should be set to true. In your mouse clicked method add the following code:
public void mouseClicked(MouseEvent e) {
    if ( drawRect) {
        Graphics g = bottomPanel.getGraphics();
        Quadrilateral shape;
        x1 = e.getX();
        y1 = e.getY();
        width = Integer.parseInt(t1.getText());
        height = Integer.parseInt(t2.getText());
        if (width != height)
            shape = new Rectangle(x1,y1,width,height);
        else
            shape = new Square(x1,y1,width);
        shapeList.add(shape);
        g.setColor(Color.red);
        g.fillPolygon(shape);
        g.setColor(Color.black);
        g.drawPolygon(shape);
        System.out.println(shape);
    }
    drawRect = false;
}
Either add or modify your load and save methods to load & display and save the shapeList to a file.

Part 2
Add an Eraser tool to the software developed for the previous lab. The user clicks on the button to select the Eraser and then holds down the left mouse button and moves the mouse to erase areas of the drawing. Your code should provide

1) a small (thin) eraser,
2) a large (fat) eraser, and
3) an object delete eraser (removes any objects whose bounding rectangle surrounds the click point.

Your Eraser tool should respond to the right mouse button for mouse dragged:

public void mouseDragged(MouseEvent e) {
    if(SwingUtilities.isRightMouseButton(e))

Email your zipped project folder as an attachment.


Based on Dr. Narayan's Lab 8, Modified by Jack Tompkins