// AddPoly.java // Command to add a polygon to the drawing. // Written by Scot Drysdale to // use for VoronoiTool import java.awt.Point; public class AddPoly extends Command { private Segment currentSeg; // Current segment being constructed private PointShape pOrig = null; // starting point for polygon // When the mouse is clicked the first time, record the original point. // Each successive click create a new segment until get back to original // point. public void executeClick(Point p, Drawing dwg) { if(pOrig == null) // Starting a polygon? { pOrig = new PointShape(p); currentSeg = new Segment(p, p); dwg.setTempShape(currentSeg); } else if(pOrig.euclideanSq(p) <= 9) // Close the polygon? { currentSeg.setEndpoint2(pOrig.getPoint()); dwg.add(currentSeg); pOrig = null; // Must start a new polygon dwg.setTempShape(null); currentSeg = null; } else { // End this segment and start a new one currentSeg.setEndpoint2(p); dwg.add(currentSeg); currentSeg = new Segment(p, p); dwg.setTempShape(currentSeg); } } // Update the segment currently being added public void executeMove(Point p, Drawing dwg) { if(currentSeg != null) currentSeg.setEndpoint2(p); } }