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 6b30894c..72730303 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 @@ -124,8 +124,14 @@ private int inputRadius() { if (result == JOptionPane.OK_OPTION) { try { - return Integer.parseInt(radiusField.getText()); + int radius = Integer.parseInt(radiusField.getText()); + if (radius <= 0) { + throw new NumberFormatException("Radius must be positive."); + } + return radius; } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(canvas, "Invalid input. Please enter a positive integer for the radius.", + "Error", JOptionPane.ERROR_MESSAGE); return -1; } } 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 a64112e0..47ff39a4 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 @@ -95,7 +95,8 @@ protected void handleMousePressed(Point2D polySpaceMousePosition) { final Point2D firstFocus = preview.getPlottedPoints().get(0); final Point2D secondFocus = preview.getPlottedPoints().get(1); - int[][] coordinates = ellipseCalculator.getOrderedPointsWithRadius(firstFocus, secondFocus, radii[0], radii[1]); + int[][] coordinates = ellipseCalculator.getOrderedPointsWithRadius(firstFocus, secondFocus, radii[0], + radii[1]); if (coordinates.length == 2) { Polygon ellipse = new Polygon(coordinates[0], coordinates[1], coordinates[0].length); @@ -140,8 +141,13 @@ private int[] inputRadii() { try { int rx = Integer.parseInt(rxField.getText()); int ry = Integer.parseInt(ryField.getText()); - return new int[]{rx, ry}; + if (rx <= 0 || ry <= 0) { + throw new NumberFormatException("Radii must be positive."); + } + return new int[] { rx, ry }; } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(canvas, "Invalid input. Please enter positive integers for the radii.", + "Error", JOptionPane.ERROR_MESSAGE); return null; } } 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 8541d52b..6c0a987e 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 @@ -104,8 +104,15 @@ private int inputVertices() { if (result == JOptionPane.OK_OPTION) { try { - return Integer.parseInt(numSidesField.getText()); + int numSides = Integer.parseInt(numSidesField.getText()); + if (numSides < 3) { + throw new NumberFormatException("Number of sides must be at least 3."); + } + return numSides; } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(canvas, + "Invalid input. Please enter an integer greater than or equal to 3 for the number of vertices.", + "Error", JOptionPane.ERROR_MESSAGE); return -1; } } 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 3a35d961..fe0d142a 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 @@ -80,6 +80,8 @@ private RotationDetails requestRotationDetails() { boolean isClockwise = clockwiseButton.isSelected(); return new RotationDetails(angle, new Point2D.Double(pivotX, pivotY), isClockwise); } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(canvas, "Invalid input! Please enter valid numbers.", "Error", + JOptionPane.ERROR_MESSAGE); // Handle invalid input gracefully, return default rotation details return new RotationDetails(0, new Point2D.Double(0, 0), false); } 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..9c40c85b 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 @@ -68,6 +68,8 @@ private Point2D requestScalingPoint() { 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 } } @@ -98,6 +100,8 @@ private double[] requestScalingFactors() { double sy = Double.parseDouble(syField.getText()); return new double[] { 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 } } 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 511212bf..edaf54fb 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 @@ -75,6 +75,8 @@ private double[] requestShearFactors() { return shearFactors; } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(canvas, "Invalid input! Please enter valid numbers.", "Error", + JOptionPane.ERROR_MESSAGE); return shearFactors; } } diff --git a/src/main/java/com/github/creme332/controller/canvas/transform/Translator.java b/src/main/java/com/github/creme332/controller/canvas/transform/Translator.java index 60de9aa3..90a53cb6 100644 --- a/src/main/java/com/github/creme332/controller/canvas/transform/Translator.java +++ b/src/main/java/com/github/creme332/controller/canvas/transform/Translator.java @@ -72,8 +72,12 @@ private Point2D requestTranslationVector() { if (result == JOptionPane.OK_OPTION) { try { - return new Point2D.Double(Integer.parseInt(rxField.getText()), Integer.parseInt(ryField.getText())); + double x = Double.parseDouble(rxField.getText()); + double y = Double.parseDouble(ryField.getText()); + return new Point2D.Double(x, y); } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(canvas, "Invalid input! Please enter valid numbers.", "Error", + JOptionPane.ERROR_MESSAGE); return zeroVector; } }