Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active March 29, 2020 16:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bytecodeman/c015be72d6b400c3a8f9b7c11acfc5c0 to your computer and use it in GitHub Desktop.
Save bytecodeman/c015be72d6b400c3a8f9b7c11acfc5c0 to your computer and use it in GitHub Desktop.
Updatable Polygon with Statistics
// Updatable Polygon Homework V2
// Antonio C. Silvestri
// CSC-112 Intermediate Java Programming
// 04/03/2018
package polygon;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class UpdatePolygonApp extends Application {
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new AppGUI(), 575, 575);
primaryStage.setTitle("Updatable Polygon By A.C. Silvestri");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main method is only needed for the IDE with limited JavaFX support. Not
* needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
class AppGUI extends BorderPane {
public AppGUI() {
UpdatablePolygonPane updatePolyPane = new UpdatablePolygonPane();
StatisticsPane statsPane = new StatisticsPane();
SizeControls hBox = new SizeControls(updatePolyPane, statsPane);
this.setCenter(updatePolyPane);
this.setRight(statsPane);
this.setBottom(hBox);
}
}
class SizeControls extends HBox {
public SizeControls(UpdatablePolygonPane updatePolyPane,
StatisticsPane statsPane) {
this.setSpacing(10);
this.setAlignment(Pos.CENTER);
Button btEnlarge = new Button("+");
Button btShrink = new Button("-");
this.getChildren().add(btEnlarge);
this.getChildren().add(btShrink);
btEnlarge.setOnAction(x -> {
updatePolyPane.enlarge();
statsPane.updateStats(updatePolyPane);
});
btShrink.setOnAction(x -> {
updatePolyPane.shrink();
statsPane.updateStats(updatePolyPane);
});
statsPane.updateStats(updatePolyPane);
}
}
class UpdatablePolygonPane extends StackPane {
private int sides = 4;
private double radius;
public int getSides() {
return this.sides;
}
public double getRadius() {
return this.radius;
}
public UpdatablePolygonPane() {
this.radius = 200;
this.paint();
}
public void shrink() {
if (this.sides > 2)
this.sides--;
this.paint();
}
public void enlarge() {
this.sides++;
this.paint();
}
private void paint() {
this.getChildren().clear();
double centerX = this.getWidth() / 2;
double centerY = this.getHeight() / 2;
Polygon poly = new Polygon();
poly.setFill(Color.WHITE);
poly.setStroke(Color.BLACK);
ObservableList<Double> list = poly.getPoints();
// Add points to the polygon list
for (int i = 0; i < this.sides; i++) {
list.add(centerX + this.radius * Math.cos(2 * i * Math.PI / this.sides));
list.add(centerY - this.radius * Math.sin(2 * i * Math.PI / this.sides));
}
this.getChildren().add(poly);
}
}
class StatisticsPane extends HBox {
private TextField txtNoOfSides;
private TextField txtPolyArea;
private TextField txtCircleArea;
private TextField txtAreaDelta;
public StatisticsPane() {
this.setPrefWidth(150);
VBox vbox = new VBox(10);
vbox.setPadding(new Insets(0, 10, 0, 10));
vbox.setAlignment(Pos.CENTER);
VBox vboxNoOfSidesGroup = new VBox(1);
txtNoOfSides = new TextField();
txtNoOfSides.setEditable(false);
vboxNoOfSidesGroup.getChildren()
.addAll(new Label("No Of Sides"), txtNoOfSides);
VBox vboxPolyAreaGroup = new VBox(1);
txtPolyArea = new TextField();
txtPolyArea.setEditable(false);
vboxPolyAreaGroup.getChildren()
.addAll(new Label("PolyArea"), txtPolyArea);
VBox vboxCircleAreaGroup = new VBox(1);
txtCircleArea = new TextField();
txtCircleArea.setEditable(false);
vboxCircleAreaGroup.getChildren()
.addAll(new Label("Circle Area"), txtCircleArea);
VBox vBoxAreaDeltaGroup = new VBox(1);
txtAreaDelta = new TextField();
txtAreaDelta.setEditable(false);
vBoxAreaDeltaGroup.getChildren()
.addAll(new Label("Area Delta"), txtAreaDelta);
vbox.getChildren().addAll(vboxNoOfSidesGroup, vboxPolyAreaGroup, vboxCircleAreaGroup, vBoxAreaDeltaGroup);
this.getChildren().addAll(
new Separator(Orientation.VERTICAL), vbox);
}
public void updateStats(UpdatablePolygonPane upp) {
int sides = upp.getSides();
double radius = upp.getRadius();
this.txtNoOfSides.setText(Integer.toString(sides));
double polyArea = sides * radius * radius * Math.sin(2 * Math.PI / sides) / 2.0;
this.txtPolyArea.setText(String.format("%.5f", polyArea));
double circleArea = Math.PI * radius * radius;
this.txtCircleArea.setText(String.format("%.5f", circleArea));
double delta = circleArea - polyArea;
this.txtAreaDelta.setText(String.format("%.5f", delta));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment