// AddRect.java // Command class to add a Rect object to the drawing. // Written by THC for CS 5 Lab Assignment 3. import java.awt.*; public class AddRect extends Command { private Point firstPoint; // starting corner for the Rect // When the mouse is pressed, remember its position, create a new Rect with // the mouse position as its upper left and zero width and height, and add // the new Rect to the front of the drawing. public void executePress(Point p, Drawing dwg) { firstPoint = p; Rect r = new Rect(p.x, p.y, 0, 0, dwg.getColor()); dwg.add(r); } // When the mouse is dragged, make a new Rect with the original press // position as one corner and the current mouse position as the // opposite corner, and have this new Rect replace the one that is // currently at the front of the drawing. public void executeDrag(Point p, Drawing dwg) { Rect r = new Rect(Math.min(p.x, firstPoint.x), Math.min(p.y, firstPoint.y), Math.abs(p.x - firstPoint.x), Math.abs(p.y - firstPoint.y), dwg.getColor()); dwg.replaceFront(r); } }