Skip to content

Commit

Permalink
Merge pull request #199 from Vasheel/dialog
Browse files Browse the repository at this point in the history
display error dialog when user enters invalid input
  • Loading branch information
creme332 authored Aug 7, 2024
2 parents eb39624 + 16f1012 commit 9bf21e2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 9bf21e2

Please sign in to comment.