Skip to content

Commit

Permalink
Merge pull request #118 from Divyesh000/polygon-controller
Browse files Browse the repository at this point in the history
complete draw regular polygon controller
  • Loading branch information
creme332 authored Jun 25, 2024
2 parents ddcec79 + e9eccc9 commit 2af5b83
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.awt.Polygon;
import java.awt.geom.Point2D;

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;
Expand All @@ -12,6 +15,7 @@
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);
Expand All @@ -24,7 +28,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();
Expand All @@ -34,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;
}

Expand All @@ -51,4 +68,28 @@ public void handleMousePressed(Point2D polySpaceMousePosition) {
public boolean shouldDraw() {
return getCanvasMode() == Mode.DRAW_REGULAR_POLYGON;
}

/**
* 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("Vertices:"));
panel.add(numSidesField);

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) {
return -1;
}
}
return -1;
}
}

0 comments on commit 2af5b83

Please sign in to comment.