diff --git a/src/main/java/com/github/creme332/controller/canvas/drawing/DrawCircle.java b/src/main/java/com/github/creme332/controller/canvas/drawing/DrawCircle.java index 7273030..fb210b5 100644 --- a/src/main/java/com/github/creme332/controller/canvas/drawing/DrawCircle.java +++ b/src/main/java/com/github/creme332/controller/canvas/drawing/DrawCircle.java @@ -116,7 +116,7 @@ private int inputRadius() { panel.add(new JLabel("Radius:")); panel.add(radiusField); - int result = JOptionPane.showConfirmDialog(null, panel, "Circle: Center & Radius", JOptionPane.OK_CANCEL_OPTION, + int result = JOptionPane.showConfirmDialog(canvas, panel, "Circle: Center & Radius", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); // request focus again otherwise keyboard shortcuts will not work diff --git a/src/main/java/com/github/creme332/controller/canvas/drawing/DrawEllipse.java b/src/main/java/com/github/creme332/controller/canvas/drawing/DrawEllipse.java index 47ff39a..76d878a 100644 --- a/src/main/java/com/github/creme332/controller/canvas/drawing/DrawEllipse.java +++ b/src/main/java/com/github/creme332/controller/canvas/drawing/DrawEllipse.java @@ -131,7 +131,7 @@ private int[] inputRadii() { panel.add(new JLabel("Radius Y:")); panel.add(ryField); - int result = JOptionPane.showConfirmDialog(null, panel, "Ellipse: Foci & Radius", JOptionPane.OK_CANCEL_OPTION, + int result = JOptionPane.showConfirmDialog(canvas, panel, "Ellipse: Foci & Radius", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); // Request focus again otherwise keyboard shortcuts will not work diff --git a/src/main/java/com/github/creme332/controller/canvas/drawing/DrawRegularPolygon.java b/src/main/java/com/github/creme332/controller/canvas/drawing/DrawRegularPolygon.java index f27c053..dcc8cba 100644 --- a/src/main/java/com/github/creme332/controller/canvas/drawing/DrawRegularPolygon.java +++ b/src/main/java/com/github/creme332/controller/canvas/drawing/DrawRegularPolygon.java @@ -14,9 +14,7 @@ import com.github.creme332.view.Canvas; public class DrawRegularPolygon extends AbstractDrawer { - PolygonCalculator calculator = new PolygonCalculator(); - private int numSides = 5; // default to 5 sides (pentagon) private Point2D firstVertex = null; private Point2D secondVertex = null; @@ -56,7 +54,7 @@ protected void handleMousePressed(Point2D polySpaceMousePosition) { preview.getPlottedPoints().add(polySpaceMousePosition); // ask user to enter number of sides - numSides = inputVertices(); + int numSides = inputVertices(); if (numSides < 3) { // invalid input => reset state and cancel operation @@ -96,7 +94,7 @@ private int inputVertices() { panel.add(new JLabel("Vertices:")); panel.add(numSidesField); - int result = JOptionPane.showConfirmDialog(null, panel, "Regular Polygon", JOptionPane.OK_CANCEL_OPTION, + int result = JOptionPane.showConfirmDialog(canvas, panel, "Regular Polygon", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); // request focus again otherwise keyboard shortcuts will not work diff --git a/src/main/java/com/github/creme332/controller/canvas/transform/Rotator.java b/src/main/java/com/github/creme332/controller/canvas/transform/Rotator.java index fe0d142..f96cdda 100644 --- a/src/main/java/com/github/creme332/controller/canvas/transform/Rotator.java +++ b/src/main/java/com/github/creme332/controller/canvas/transform/Rotator.java @@ -66,7 +66,7 @@ private RotationDetails requestRotationDetails() { panel.add(counterClockwiseButton); panel.add(clockwiseButton); - int result = JOptionPane.showConfirmDialog(null, panel, "Rotate About Point", + int result = JOptionPane.showConfirmDialog(canvas, panel, "Rotate About Point", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); // Request focus again otherwise keyboard shortcuts will not work diff --git a/src/main/java/com/github/creme332/controller/canvas/transform/Scaler.java b/src/main/java/com/github/creme332/controller/canvas/transform/Scaler.java index 9c40c85..c748c23 100644 --- a/src/main/java/com/github/creme332/controller/canvas/transform/Scaler.java +++ b/src/main/java/com/github/creme332/controller/canvas/transform/Scaler.java @@ -26,11 +26,18 @@ public void handleShapeSelection(int shapeIndex) { ShapeWrapper selectedShape = canvasModel.getShapeManager().getShapeByIndex(shapeIndex); // Request user for the scaling point and scaling factors - Point2D scalingPoint = requestScalingPoint(); - double[] scalingFactors = requestScalingFactors(); + double[] scaleData = requestScaleData(); + if (scaleData.length != 4) { + return; // User cancelled the dialog + } + + // Extract scaling point and scaling factors + Point2D scalingPoint = new Point2D.Double(scaleData[0], scaleData[1]); + double sx = scaleData[2]; + double sy = scaleData[3]; // Scale the shape - selectedShape.scale(scalingPoint, scalingFactors[0], scalingFactors[1]); + selectedShape.scale(scalingPoint, sx, sy); // Replace old shape with new one canvasModel.getShapeManager().editShape(shapeIndex, selectedShape); @@ -45,66 +52,45 @@ public boolean shouldDraw() { } /** - * Requests user to enter the coordinates of the scaling point. + * Requests user to enter the coordinates of the scaling point and scaling + * factors. * - * @return The scaling point. + * @return An array containing [x, y, sx, sy] or null if the input is invalid or + * cancelled. */ - private Point2D requestScalingPoint() { + private double[] requestScaleData() { JTextField xField = new JTextField(5); JTextField yField = new JTextField(5); + JTextField sxField = new JTextField(5); + JTextField syField = new JTextField(5); + JPanel panel = new JPanel(); panel.add(new JLabel("X:")); panel.add(xField); panel.add(new JLabel("Y:")); panel.add(yField); - - int result = JOptionPane.showConfirmDialog(null, panel, "Enter coordinates of scaling point", - JOptionPane.OK_CANCEL_OPTION, - JOptionPane.PLAIN_MESSAGE); - - if (result == JOptionPane.OK_OPTION) { - try { - double x = Double.parseDouble(xField.getText()); - double y = Double.parseDouble(yField.getText()); - return new Point2D.Double(x, y); - } catch (NumberFormatException e) { - JOptionPane.showMessageDialog(canvas, "Invalid input! Please enter valid numbers.", "Error", - JOptionPane.ERROR_MESSAGE); - return new Point2D.Double(0, 0); // Default value if input is invalid - } - } - return new Point2D.Double(0, 0); // Default value if canceled - } - - /** - * Requests user to enter the scaling factors. - * - * @return The scaling factors [sx, sy]. - */ - private double[] requestScalingFactors() { - JTextField sxField = new JTextField(5); - JTextField syField = new JTextField(5); - JPanel panel = new JPanel(); panel.add(new JLabel("Scale X:")); panel.add(sxField); panel.add(new JLabel("Scale Y:")); panel.add(syField); - int result = JOptionPane.showConfirmDialog(null, panel, "Enter scaling factors", + int result = JOptionPane.showConfirmDialog(canvas, panel, "Enter scaling data", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { try { + double x = Double.parseDouble(xField.getText()); + double y = Double.parseDouble(yField.getText()); double sx = Double.parseDouble(sxField.getText()); double sy = Double.parseDouble(syField.getText()); - return new double[] { sx, sy }; + return new double[] { x, y, sx, sy }; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(canvas, "Invalid input! Please enter valid numbers.", "Error", JOptionPane.ERROR_MESSAGE); - return new double[] { 1.0, 1.0 }; // Default scaling factors if input is invalid + return new double[] {}; } } - return new double[] { 1.0, 1.0 }; // Default scaling factors if canceled + return new double[] {}; } -} +} \ No newline at end of file diff --git a/src/main/java/com/github/creme332/controller/canvas/transform/Shearer.java b/src/main/java/com/github/creme332/controller/canvas/transform/Shearer.java index edaf54f..2063cad 100644 --- a/src/main/java/com/github/creme332/controller/canvas/transform/Shearer.java +++ b/src/main/java/com/github/creme332/controller/canvas/transform/Shearer.java @@ -59,7 +59,7 @@ private double[] requestShearFactors() { panel.add(new JLabel("Shear Y:")); panel.add(syField); - int result = JOptionPane.showConfirmDialog(null, panel, "Enter shear factors", + int result = JOptionPane.showConfirmDialog(canvas, panel, "Enter shear factors", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);