// DeleteAreaCmd.java // Deletes all shapes lying entirely in a dragged rectangle. // by Scot Drysdale on 7/12/06 import java.awt.Color; import java.awt.Point; public class DeleteAreaCmd extends Command { private Point firstPoint = null; // starting corner for the Rect private Rect currentRect; // When the mouse is clicked, remember its position, create a new Rect with // the mouse position as its upper left and zero width and height, and make // the rect the temporary shape. The next time clicked, delete all shapes // contained in the rectangle. public void executeClick(Point p, Drawing dwg) { if(firstPoint == null) { firstPoint = p; currentRect = new Rect(p.x, p.y, 0, 0, Color.blue); dwg.setTempShape(currentRect); } else // Second click, so time to remove things in rectangle { dwg.removeInRect(currentRect); firstPoint = null; dwg.setTempShape(null); } } // When the mouse is moved, 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 current one public void executeMove(Point p, Drawing dwg) { currentRect = 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), Color.blue); dwg.setTempShape(currentRect); } }