Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Omar Natour
- * 11/18/16
- * Csc-220 Data Structures
- * Hw7 Koch Snowflake
- * Use recursion to make a koch snowflake in Javafx
- * Ojnatour0001@student.stcc.edu
- */
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- public class SnowFlake extends Application {
- public static void main(String[] args) {
- launch(args);
- }
- BorderPane bPane = new BorderPane();
- Scene sce = new Scene(bPane, 450, 450);
- TextField tfStatus = new TextField();
- int order = 0;
- TextField tfOrder = new TextField();
- public void start(Stage PrimaryStage) {
- bPane.setCenter(center());
- bPane.setBottom(bottom());
- bPane.setTop(top());
- PrimaryStage.setTitle("Koch Snowflake");
- PrimaryStage.setScene(sce);
- PrimaryStage.show();
- PrimaryStage.setResizable(true);
- PrimaryStage.setMinHeight(400);
- PrimaryStage.setMinWidth(400);
- sce.widthProperty().addListener(ov -> pane.paint());
- sce.heightProperty().addListener(ov -> pane.paint());
- }
- private Node bottom() {
- HBox hBox = new HBox();
- hBox.setPadding(new Insets(10, 10, 10, 10));
- hBox.setAlignment(Pos.CENTER);
- hBox.setMinHeight((25));
- tfStatus.setPromptText("Status");
- tfStatus.setEditable(false);
- tfStatus.setPrefColumnCount(20);
- tfStatus.setAlignment(Pos.CENTER);
- hBox.getChildren().add(tfStatus);
- return hBox;
- }
- SnowFlakePane pane = new SnowFlakePane();
- private Node center() {
- Rectangle rOutline = new Rectangle();
- rOutline.setFill(Color.TRANSPARENT);
- rOutline.setStroke(Color.BLACK);
- rOutline.setArcHeight(10);
- rOutline.setArcWidth(10);
- rOutline.setStrokeWidth(1);
- rOutline.heightProperty().bind(sce.widthProperty().multiply(.8));
- rOutline.widthProperty().bind(sce.widthProperty().multiply(.9));
- return pane;
- }
- private Node top() {
- HBox hBox = new HBox(5);
- Button bMinus = new Button("-");
- Button bPlus = new Button("+");
- tfOrder.setPromptText("Order of Snowflake");
- tfOrder.setPrefColumnCount(12);
- tfOrder.setAlignment(Pos.CENTER);
- bPlus.setPrefSize(55, 25);
- bMinus.setPrefSize(55, 25);
- hBox.setAlignment(Pos.CENTER);
- hBox.setPadding(new Insets(10, 10, 10, 10));
- bPlus.setOnAction(Plus);
- bMinus.setOnAction(Minus);
- tfOrder.setOnAction(Enter);
- hBox.getChildren().addAll(bMinus, tfOrder, bPlus);
- return hBox;
- }
- EventHandler<ActionEvent> Enter = e -> {
- if (tfOrder.getText().matches("^ *\\d+ *$")) {
- order = Integer.parseInt(tfOrder.getText().trim());
- tfStatus.setText("" + order);
- pane.setOrder(order);
- } else if (tfOrder.getText().matches("^ *-\\d+ *$"))
- tfStatus.setText("Error, cannot set order as a negative value.");
- else
- tfStatus.setText("Error.");
- };
- EventHandler<ActionEvent> Minus = e -> {
- if (order > 0) {
- order--;
- tfStatus.setText("" + order);
- pane.setOrder(order);
- } else {
- tfStatus.setText("Error, cannot set order as a negative value.");
- }
- };
- EventHandler<ActionEvent> Plus = e -> {
- order++;
- tfStatus.setText("" + order);
- pane.setOrder(order);
- };
- }
- class SnowFlakePane extends Pane {
- private int order = 0;
- private double startX0 = 0.;
- private double startX1 = 0.;
- private double startX2 = 0.;
- private double startY0 = 0.;
- private double startY1 = 0.;
- private double startY2 = 0.;
- public void setOrder(int order) {
- this.order = order;
- paint();
- }
- public void paint() {
- startX0 = (getWidth() / 6);
- startX1 = (getWidth() - getWidth() / 6);
- startX2 = (getWidth() / 2);
- startY0 = (getHeight() / 2.7
- + Math.sqrt(Math.pow(startX1 - startX0, 2) - Math.pow(((startX1 - startX0) / 2), 2)) / 2);
- startY1 = startY0;
- startY2 = startY0 - Math.sqrt(Math.pow(startX1 - startX0, 2) - Math.pow(((startX1 - startX0) / 2), 2));
- getChildren().clear();
- paintKochCurve(order, startX0, startY0, startX1, startY1);
- paintKochCurve(order, startX1, startY1, startX2, startY2);
- paintKochCurve(order, startX2, startY2, startX0, startY0);
- }
- private void paintKochCurve(int order, double x0, double y0, double x1, double y1) {
- if (order == 0)
- getChildren().add(new Line(x0, y0, x1, y1));
- else {
- double width = x1 - x0;
- double height = y1 - y0;
- double fx = width / 3 + x0;
- double fy = height / 3 + y0;
- double px = (0.5 * (x0 + x1) + Math.sqrt(3) * (y0 - y1) / 6);
- double py = (0.5 * (y0 + y1) + Math.sqrt(3) * (x1 - x0) / 6);
- double lx = (2 * width) / 3 + x0;
- double ly = (2 * height) / 3 + y0;
- paintKochCurve(order - 1, x0, y0, fx, fy);
- paintKochCurve(order - 1, fx, fy, px, py);
- paintKochCurve(order - 1, px, py, lx, ly);
- paintKochCurve(order - 1, lx, ly, x1, y1);
- }
- }
- public SnowFlakePane() {
- this.setOnMouseDragged(e -> {
- try {
- if (((Node) this.getChildren()).isVisible()) {
- startX0 = (e.getX() / 6);
- startX1 = (e.getX() - e.getX() / 6);
- startX2 = (e.getX() / 2);
- startY0 = (e.getY() - e.getY() / 2.7);
- startY2 = startY0 - Math.sqrt(Math.pow(startX1 - startX0, 2) - Math.pow(((startX1 - startX0) / 2), 2));
- paint();
- }
- } catch (ClassCastException e1) {
- // Just so clicking and dragging on the window does not
- // prematurely create a Koch curve.
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement