From 066bfca6a4cb508650027d9d73e4ecac74252c95 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Tue, 25 Jun 2024 08:57:05 +0400 Subject: [PATCH 1/2] complete draw regular polygon controller --- .../drawing/DrawRegularPolygon.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java b/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java index a829e6f2..f01fbe46 100644 --- a/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java +++ b/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java @@ -2,7 +2,11 @@ import java.awt.Polygon; import java.awt.geom.Point2D; - +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JOptionPane; +import javax.swing.JTextField; import com.github.creme332.algorithms.PolygonCalculator; import com.github.creme332.model.AppState; import com.github.creme332.model.Mode; @@ -12,9 +16,11 @@ public class DrawRegularPolygon extends DrawController { PolygonCalculator calculator = new PolygonCalculator(); + private int numSides = 5; // default to 5 sides (pentagon) public DrawRegularPolygon(AppState app, Canvas canvas) { super(app, canvas); + numSides = getNumSidesFromUser(); } @Override @@ -24,7 +30,7 @@ public void handleMouseMoved(Point2D polySpaceMousePosition) { Point2D center = preview.getPlottedPoints().get(0); int side = (int) Math.abs(center.distance(polySpaceMousePosition)); - int[][] res = calculator.getOrderedPoints(5, side, (int) center.getX(), (int) center.getY()); + int[][] res = calculator.getOrderedPoints(numSides, side, (int) center.getX(), (int) center.getY()); Polygon p = new Polygon(res[0], res[1], res[0].length); preview.setShape(p); canvas.repaint(); @@ -51,4 +57,22 @@ public void handleMousePressed(Point2D polySpaceMousePosition) { public boolean shouldDraw() { return getCanvasMode() == Mode.DRAW_REGULAR_POLYGON; } + + private int getNumSidesFromUser() { + JTextField numSidesField = new JTextField(5); + JPanel panel = new JPanel(); + panel.add(new JLabel("Enter number of sides:")); + panel.add(numSidesField); + + int result = JOptionPane.showConfirmDialog(null, panel, "Number of Sides", JOptionPane.OK_CANCEL_OPTION, + JOptionPane.PLAIN_MESSAGE); + if (result == JOptionPane.OK_OPTION) { + try { + return Integer.parseInt(numSidesField.getText()); + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(null, "Invalid input. Defaulting to 5 sides."); + } + } + return 5; // default to 5 sides if input is invalid or cancelled + } } From e9eccc9f50b7a8fd54546dfc02a27a97db34d36d Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:30:00 +0400 Subject: [PATCH 2/2] show dialog only after first point has been selected., cancel operation for invalid values --- .../drawing/DrawRegularPolygon.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java b/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java index f01fbe46..3f658786 100644 --- a/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java +++ b/src/main/java/com/github/creme332/controller/drawing/DrawRegularPolygon.java @@ -2,7 +2,6 @@ import java.awt.Polygon; import java.awt.geom.Point2D; -import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JOptionPane; @@ -20,7 +19,6 @@ public class DrawRegularPolygon extends DrawController { public DrawRegularPolygon(AppState app, Canvas canvas) { super(app, canvas); - numSides = getNumSidesFromUser(); } @Override @@ -40,11 +38,24 @@ public void handleMouseMoved(Point2D polySpaceMousePosition) { @Override public void handleMousePressed(Point2D polySpaceMousePosition) { if (preview == null) { + // center of polygon has been input + + // initialize shape preview preview = new ShapeWrapper(canvasModel.getFillColor(), canvasModel.getFillColor(), canvasModel.getLineType(), canvasModel.getLineThickness()); preview.getPlottedPoints().add(polySpaceMousePosition); + + // add preview to model canvasModel.getShapes().add(preview); + + // ask user to enter number of sides + numSides = inputVertices(); + + if (numSides < 3) { + // invalid input => cancel operation + disposePreview(); + } return; } @@ -58,21 +69,27 @@ public boolean shouldDraw() { return getCanvasMode() == Mode.DRAW_REGULAR_POLYGON; } - private int getNumSidesFromUser() { + /** + * Asks user to enter number of vertices for polygon. If input value is invalid + * or if operation is cancelled, -1 is returned. + * + * @return + */ + private int inputVertices() { JTextField numSidesField = new JTextField(5); JPanel panel = new JPanel(); - panel.add(new JLabel("Enter number of sides:")); + panel.add(new JLabel("Vertices:")); panel.add(numSidesField); - int result = JOptionPane.showConfirmDialog(null, panel, "Number of Sides", JOptionPane.OK_CANCEL_OPTION, + int result = JOptionPane.showConfirmDialog(null, panel, "Regular Polygon", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { try { return Integer.parseInt(numSidesField.getText()); } catch (NumberFormatException e) { - JOptionPane.showMessageDialog(null, "Invalid input. Defaulting to 5 sides."); + return -1; } } - return 5; // default to 5 sides if input is invalid or cancelled + return -1; } }