@Override public void mouseDragged(MouseEvent e, CS355Drawing model, Color c) { // Get shape at saved index from model and verify it is a circle Shape shape = model.getShape(this.index); if (!(shape instanceof cs355.model.drawing.Circle)) { GUIFunctions.printf( "Invalid shape - expected cs355.model.drawing.Circle at index %d", this.index); return; } // Cast the shape to a circle and save the new coordinates Circle circle = (Circle) shape; // Initialize new center coordinates to initial coordinates Point2D.Double center = new Point2D.Double(); center.setLocation(this.initialCoordinates); // Get current pointer coordinates Point2D.Double currentCoordinates = new Point2D.Double(); currentCoordinates.setLocation(e.getPoint()); // Find difference between pointer and initial coordinates to get correct orientation double xDifference = currentCoordinates.getX() - initialCoordinates.getX(); double yDifference = currentCoordinates.getY() - initialCoordinates.getY(); // Get radius double radius = Math.min(Math.abs(xDifference), Math.abs(yDifference)) / 2.0; // Get unit vectors for the differences to preserve sign double xDirection = xDifference / Math.abs(xDifference); double yDirection = yDifference / Math.abs(yDifference); // Calculate position of the center of the circle center.x = this.initialCoordinates.getX() + (xDirection * radius); center.y = this.initialCoordinates.getY() + (yDirection * radius); // Set the new parameters circle.setCenter(center); circle.setRadius(radius); }
@Override public void mouseDragged(MouseEvent e) { AffineTransform viewToWorld = new AffineTransform(1, 0, 0, 1, viewPoint.getX(), viewPoint.getY()); AffineTransform scale = new AffineTransform(this.getScale(), 0, 0, this.getScale(), 0, 0); viewToWorld.concatenate(scale); p2 = new Point2D.Double(e.getPoint().getX(), e.getPoint().getY()); viewToWorld.transform(p2, p2); if (button == "line") { currentShape = model.getShape(model.getSize() - 1); Line l = (Line) currentShape; l.setEnd(p2); model.deleteShape(model.getSize() - 1); model.addShape(l); } else if (button == "square") { double size = Math.min((Math.abs(p1.getX() - p2.getX())), (Math.abs(p1.getY() - p2.getY()))); Point2D.Double upLeft = new Point2D.Double(); if (p1.getX() <= p2.getX() && p1.getY() <= p2.getY()) { upLeft.setLocation(p1.getX(), p1.getY()); } else if (p1.getX() <= p2.getX() && p1.getY() > p2.getY()) { upLeft.setLocation(p1.getX(), p1.getY() - size); } else if (p1.getX() > p2.getX() && p1.getY() <= p2.getY()) { upLeft.setLocation(p1.getX() - size, p1.getY()); } else { upLeft.setLocation(p1.getX() - size, p1.getY() - size); } Point2D.Double center = new Point2D.Double(upLeft.getX() + size / 2, upLeft.getY() + size / 2); currentShape = model.getShape(model.getSize() - 1); Square s = (Square) currentShape; s.setCenter(center); s.setSize(size); model.deleteShape(model.getSize() - 1); model.addShape(s); } else if (button == "rectangle") { Point2D.Double upLeft = new Point2D.Double(); upLeft.setLocation(Math.min(p1.getX(), p2.getX()), Math.min(p1.getY(), p2.getY())); Double width = Math.abs(p1.getX() - p2.getX()); Double height = Math.abs(p1.getY() - p2.getY()); currentShape = model.getShape(model.getSize() - 1); Rectangle r = (Rectangle) currentShape; Point2D.Double center = new Point2D.Double(upLeft.getX() + width / 2, upLeft.getY() + height / 2); r.setHeight(height); r.setWidth(width); r.setCenter(center); model.deleteShape(model.getSize() - 1); model.addShape(r); } else if (button == "circle") { double size = Math.min((Math.abs(p1.getX() - p2.getX())), (Math.abs(p1.getY() - p2.getY()))); double radius = size / 2; Point2D.Double upLeft = new Point2D.Double(); if (p1.getX() <= p2.getX() && p1.getY() <= p2.getY()) { upLeft.setLocation(p1.getX(), p1.getY()); } else if (p1.getX() <= p2.getX() && p1.getY() > p2.getY()) { upLeft.setLocation(p1.getX(), p1.getY() - size); } else if (p1.getX() > p2.getX() && p1.getY() <= p2.getY()) { upLeft.setLocation(p1.getX() - size, p1.getY()); } else { upLeft.setLocation(p1.getX() - size, p1.getY() - size); } Point2D.Double center = new Point2D.Double(upLeft.getX() + radius, upLeft.getY() + radius); currentShape = model.getShape(model.getSize() - 1); Circle c = (Circle) currentShape; c.setCenter(center); c.setRadius(radius); model.deleteShape(model.getSize() - 1); model.addShape(c); } else if (button == "ellipse") { Point2D.Double center = new Point2D.Double((p1.getX() + p2.getX()) / 2, (p1.getY() + p2.getY()) / 2); Double width = Math.abs(p1.getX() - p2.getX()); Double height = Math.abs(p1.getY() - p2.getY()); currentShape = model.getShape(model.getSize() - 1); Ellipse el = (Ellipse) currentShape; el.setCenter(center); el.setHeight(height); el.setWidth(width); model.deleteShape(model.getSize() - 1); model.addShape(el); } else if (button == "select" && selectedIndex > -1) { if (handleSelected) { AffineTransform worldToObj = new AffineTransform( 1, 0, 0, 1, -selectedShape.getCenter().getX(), -selectedShape.getCenter().getY()); Point2D.Double objCoord = new Point2D.Double(); worldToObj.transform(p2, objCoord); if (selectedShape instanceof Line) { Line l = (Line) selectedShape; if (lineHandleSelected == 1) { Point2D.Double newCenter = new Point2D.Double(p2.getX(), p2.getY()); l.setCenter(newCenter); } else if (lineHandleSelected == 2) { Point2D.Double newEnd = new Point2D.Double(p2.getX(), p2.getY()); l.setEnd(newEnd); } selectedShape = l; model.setShape(selectedIndex, selectedShape); } else { double theta = Math.acos( -objCoord.getY() / Math.sqrt(Math.pow(objCoord.getX(), 2) + Math.pow(-objCoord.getY(), 2))); if (objCoord.getX() < 0) { theta = -theta; } selectedShape.setRotation(theta); model.setShape(selectedIndex, selectedShape); } } else { Point2D.Double newCenter = new Point2D.Double((p2.getX() - diff.getX()), (p2.getY() - diff.getY())); if (selectedShape instanceof Line) { Line l = (Line) selectedShape; Point2D.Double len = new Point2D.Double( l.getEnd().getX() - l.getCenter().getX(), l.getEnd().getY() - l.getCenter().getY()); Point2D.Double newEnd = new Point2D.Double((newCenter.getX() + len.getX()), (newCenter.getY() + len.getY())); l.setEnd(newEnd); selectedShape = l; } else if (selectedShape instanceof Triangle) { Triangle t = (Triangle) selectedShape; Point2D.Double change = new Point2D.Double( newCenter.getX() - selectedShape.getCenter().getX(), newCenter.getY() - selectedShape.getCenter().getY()); t.setA( new Point2D.Double(t.getA().getX() + change.getX(), t.getA().getY() + change.getY())); t.setB( new Point2D.Double(t.getB().getX() + change.getX(), t.getB().getY() + change.getY())); t.setC( new Point2D.Double(t.getC().getX() + change.getX(), t.getC().getY() + change.getY())); selectedShape = t; } selectedShape.setCenter(newCenter); model.setShape(selectedIndex, selectedShape); } } }