@Override public void colorButtonHit(Color c) { col = c; if (selectedIndex > -1) { Shape s = model.getShape(selectedIndex); s.setColor(col); model.setShape(selectedIndex, s); } GUIFunctions.changeSelectedColor(c); }
@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 mousePressed(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); p1 = new Point2D.Double(e.getPoint().getX(), e.getPoint().getY()); viewToWorld.transform(p1, p1); if (button == "line") { currentShape = new Line(col, p1, p1); } else if (button == "square") { currentShape = new Square(col, p1, 0); } else if (button == "rectangle") { currentShape = new Rectangle(col, p1, 0, 0); } else if (button == "circle") { currentShape = new Circle(col, p1, 0); } else if (button == "ellipse") { currentShape = new Ellipse(col, p1, 0, 0); } else if (button == "triangle") { if (triangleCount == 0) { t1 = new Point2D.Double(p1.getX(), p1.getY()); triangleCount++; } else if (triangleCount == 1) { t2 = new Point2D.Double(p1.getX(), p1.getY()); triangleCount++; } else { t3 = new Point2D.Double(p1.getX(), p1.getY()); Point2D.Double center = new Point2D.Double(); center.setLocation( (t1.getX() + t2.getX() + t3.getX()) / 3, (t1.getY() + t2.getY() + t3.getY()) / 3); triangleCount = 0; Shape t = new Triangle(col, center, t1, t2, t3); model.addShape(t); } return; } else if (button == "select") { selectedIndex = geometryTest(p1, 4); if (selectedIndex > -1) { Shape s = model.getShape(selectedIndex); GUIFunctions.changeSelectedColor(s.getColor()); diff = new Point2D.Double(p1.getX() - s.getCenter().getX(), p1.getY() - s.getCenter().getY()); if (doHandleCheck(p1, selectedShape)) { handleSelected = true; if (s instanceof Line) { Line l = (Line) s; Point2D.Double len = new Point2D.Double( l.getEnd().getX() - l.getCenter().getX(), l.getEnd().getY() - l.getCenter().getY()); AffineTransform worldToObj = new AffineTransform(1, 0, 0, 1, -s.getCenter().getX(), -s.getCenter().getY()); Point2D.Double objCoord = new Point2D.Double(); worldToObj.transform(p1, objCoord); if (objCoord.getX() * objCoord.getX() + objCoord.getY() * objCoord.getY() < 100 * this.getScale()) { lineHandleSelected = 1; } else if ((objCoord.getX() - len.getX()) * (objCoord.getX() - len.getX()) + (objCoord.getY() - len.getY()) * (objCoord.getY() - len.getY()) < 100 * this.getScale()) { lineHandleSelected = 2; } } } } return; } else { return; } model.addShape(currentShape); }