Skip to content

Commit

Permalink
Merge pull request #200 from Ramoogur/inputscaling
Browse files Browse the repository at this point in the history
SingleDialogScaling
  • Loading branch information
creme332 authored Aug 7, 2024
2 parents 43d2339 + 797a818 commit 542d28d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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[] {};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 542d28d

Please sign in to comment.