From 253f1deeb405c244606b67ab8b3bc7ca9901ede4 Mon Sep 17 00:00:00 2001 From: Ramoogur Date: Mon, 5 Aug 2024 11:11:36 +0400 Subject: [PATCH 1/3] SingleDialogScaling --- .../controller/canvas/transform/Scaler.java | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) 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 24a23bf2..46f786e0 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 == null) { + 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,62 +52,41 @@ 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) { - 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(null, 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) { - return new double[] { 1.0, 1.0 }; // Default scaling factors if input is invalid + return null; // Invalid input } } - return new double[] { 1.0, 1.0 }; // Default scaling factors if canceled + return null; // Cancelled } } From e741d63077ee22f4b2140c6810ddc4c9f41b77e2 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:37:07 +0400 Subject: [PATCH 2/3] handle error when input is cancelled --- .../com/github/creme332/controller/canvas/transform/Scaler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 82601b03..c748c237 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 @@ -27,7 +27,7 @@ public void handleShapeSelection(int shapeIndex) { // Request user for the scaling point and scaling factors double[] scaleData = requestScaleData(); - if (scaleData == null) { + if (scaleData.length != 4) { return; // User cancelled the dialog } From 797a818e8ba9f32680b9a45ea0ea7c4ff92385fc Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:37:27 +0400 Subject: [PATCH 3/3] refactor --- .../creme332/controller/canvas/drawing/DrawCircle.java | 2 +- .../creme332/controller/canvas/drawing/DrawEllipse.java | 2 +- .../controller/canvas/drawing/DrawRegularPolygon.java | 6 ++---- .../creme332/controller/canvas/transform/Rotator.java | 2 +- .../creme332/controller/canvas/transform/Shearer.java | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) 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 72730303..fb210b5e 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 47ff39a4..76d878a8 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 f27c0537..dcc8cba7 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 fe0d142a..f96cdda4 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/Shearer.java b/src/main/java/com/github/creme332/controller/canvas/transform/Shearer.java index edaf54fb..2063cad7 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);