Advertisement
Omar_Natour

Natour, O. 11/17/16 Csc-220 Koch Snowflake

Nov 17th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.65 KB | None | 0 0
  1. /*Omar Natour
  2.  * 11/18/16
  3.  * Csc-220 Data Structures
  4.  * Hw7 Koch Snowflake
  5.  * Use recursion to make a koch snowflake in Javafx
  6.  * Ojnatour0001@student.stcc.edu
  7.  */
  8.  
  9. import javafx.application.Application;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.EventHandler;
  12. import javafx.geometry.Insets;
  13. import javafx.geometry.Pos;
  14. import javafx.scene.Node;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.layout.BorderPane;
  19. import javafx.scene.layout.HBox;
  20. import javafx.scene.layout.Pane;
  21. import javafx.scene.paint.Color;
  22. import javafx.scene.shape.Line;
  23. import javafx.scene.shape.Rectangle;
  24. import javafx.stage.Stage;
  25.  
  26. public class SnowFlake extends Application {
  27.     public static void main(String[] args) {
  28.         launch(args);
  29.     }
  30.  
  31.     BorderPane bPane = new BorderPane();
  32.     Scene sce = new Scene(bPane, 450, 450);
  33.     TextField tfStatus = new TextField();
  34.     int order = 0;
  35.     TextField tfOrder = new TextField();
  36.  
  37.     public void start(Stage PrimaryStage) {
  38.  
  39.         bPane.setCenter(center());
  40.         bPane.setBottom(bottom());
  41.         bPane.setTop(top());
  42.  
  43.         PrimaryStage.setTitle("Koch Snowflake");
  44.         PrimaryStage.setScene(sce);
  45.         PrimaryStage.show();
  46.         PrimaryStage.setResizable(true);
  47.         PrimaryStage.setMinHeight(400);
  48.         PrimaryStage.setMinWidth(400);
  49.  
  50.         sce.widthProperty().addListener(ov -> pane.paint());
  51.         sce.heightProperty().addListener(ov -> pane.paint());
  52.  
  53.     }
  54.  
  55.     private Node bottom() {
  56.         HBox hBox = new HBox();
  57.  
  58.         hBox.setPadding(new Insets(10, 10, 10, 10));
  59.         hBox.setAlignment(Pos.CENTER);
  60.         hBox.setMinHeight((25));
  61.  
  62.         tfStatus.setPromptText("Status");
  63.         tfStatus.setEditable(false);
  64.         tfStatus.setPrefColumnCount(20);
  65.         tfStatus.setAlignment(Pos.CENTER);
  66.  
  67.         hBox.getChildren().add(tfStatus);
  68.         return hBox;
  69.     }
  70.  
  71.     SnowFlakePane pane = new SnowFlakePane();
  72.  
  73.     private Node center() {
  74.  
  75.         Rectangle rOutline = new Rectangle();
  76.  
  77.         rOutline.setFill(Color.TRANSPARENT);
  78.         rOutline.setStroke(Color.BLACK);
  79.         rOutline.setArcHeight(10);
  80.         rOutline.setArcWidth(10);
  81.         rOutline.setStrokeWidth(1);
  82.         rOutline.heightProperty().bind(sce.widthProperty().multiply(.8));
  83.         rOutline.widthProperty().bind(sce.widthProperty().multiply(.9));
  84.  
  85.         return pane;
  86.     }
  87.  
  88.     private Node top() {
  89.  
  90.         HBox hBox = new HBox(5);
  91.  
  92.         Button bMinus = new Button("-");
  93.         Button bPlus = new Button("+");
  94.  
  95.         tfOrder.setPromptText("Order of Snowflake");
  96.         tfOrder.setPrefColumnCount(12);
  97.         tfOrder.setAlignment(Pos.CENTER);
  98.  
  99.         bPlus.setPrefSize(55, 25);
  100.         bMinus.setPrefSize(55, 25);
  101.  
  102.         hBox.setAlignment(Pos.CENTER);
  103.         hBox.setPadding(new Insets(10, 10, 10, 10));
  104.  
  105.         bPlus.setOnAction(Plus);
  106.         bMinus.setOnAction(Minus);
  107.         tfOrder.setOnAction(Enter);
  108.  
  109.         hBox.getChildren().addAll(bMinus, tfOrder, bPlus);
  110.         return hBox;
  111.     }
  112.  
  113.     EventHandler<ActionEvent> Enter = e -> {
  114.         if (tfOrder.getText().matches("^ *\\d+ *$")) {
  115.             order = Integer.parseInt(tfOrder.getText().trim());
  116.             tfStatus.setText("" + order);
  117.             pane.setOrder(order);
  118.  
  119.         } else if (tfOrder.getText().matches("^ *-\\d+ *$"))
  120.             tfStatus.setText("Error, cannot set order as a negative value.");
  121.         else
  122.             tfStatus.setText("Error.");
  123.     };
  124.     EventHandler<ActionEvent> Minus = e -> {
  125.         if (order > 0) {
  126.             order--;
  127.             tfStatus.setText("" + order);
  128.             pane.setOrder(order);
  129.         } else {
  130.             tfStatus.setText("Error, cannot set order as a negative value.");
  131.         }
  132.     };
  133.  
  134.     EventHandler<ActionEvent> Plus = e -> {
  135.         order++;
  136.         tfStatus.setText("" + order);
  137.         pane.setOrder(order);
  138.     };
  139. }
  140.  
  141. class SnowFlakePane extends Pane {
  142.     private int order = 0;
  143.  
  144.     private double startX0 = 0.;
  145.     private double startX1 = 0.;
  146.     private double startX2 = 0.;
  147.     private double startY0 = 0.;
  148.     private double startY1 = 0.;
  149.     private double startY2 = 0.;
  150.  
  151.     public void setOrder(int order) {
  152.         this.order = order;
  153.         paint();
  154.     }
  155.  
  156.     public void paint() {
  157.  
  158.         startX0 = (getWidth() / 6);
  159.         startX1 = (getWidth() - getWidth() / 6);
  160.         startX2 = (getWidth() / 2);
  161.         startY0 = (getHeight() / 2.7
  162.                 + Math.sqrt(Math.pow(startX1 - startX0, 2) - Math.pow(((startX1 - startX0) / 2), 2)) / 2);
  163.         startY1 = startY0;
  164.         startY2 = startY0 - Math.sqrt(Math.pow(startX1 - startX0, 2) - Math.pow(((startX1 - startX0) / 2), 2));
  165.  
  166.         getChildren().clear();
  167.  
  168.         paintKochCurve(order, startX0, startY0, startX1, startY1);
  169.         paintKochCurve(order, startX1, startY1, startX2, startY2);
  170.         paintKochCurve(order, startX2, startY2, startX0, startY0);
  171.  
  172.     }
  173.  
  174.     private void paintKochCurve(int order, double x0, double y0, double x1, double y1) {
  175.         if (order == 0)
  176.             getChildren().add(new Line(x0, y0, x1, y1));
  177.         else {
  178.  
  179.             double width = x1 - x0;
  180.             double height = y1 - y0;
  181.  
  182.             double fx = width / 3 + x0;
  183.             double fy = height / 3 + y0;
  184.             double px = (0.5 * (x0 + x1) + Math.sqrt(3) * (y0 - y1) / 6);
  185.             double py = (0.5 * (y0 + y1) + Math.sqrt(3) * (x1 - x0) / 6);
  186.             double lx = (2 * width) / 3 + x0;
  187.             double ly = (2 * height) / 3 + y0;
  188.  
  189.             paintKochCurve(order - 1, x0, y0, fx, fy);
  190.             paintKochCurve(order - 1, fx, fy, px, py);
  191.             paintKochCurve(order - 1, px, py, lx, ly);
  192.             paintKochCurve(order - 1, lx, ly, x1, y1);
  193.         }
  194.     }
  195.    
  196.     public SnowFlakePane() {
  197.         this.setOnMouseDragged(e -> {
  198.             try {
  199.                 if (((Node) this.getChildren()).isVisible()) {
  200.                     startX0 = (e.getX() / 6);
  201.                     startX1 = (e.getX() - e.getX() / 6);
  202.                     startX2 = (e.getX() / 2);
  203.                     startY0 = (e.getY() - e.getY() / 2.7);
  204.                     startY2 = startY0 - Math.sqrt(Math.pow(startX1 - startX0, 2) - Math.pow(((startX1 - startX0) / 2), 2));
  205.                     paint();
  206.                 }
  207.             } catch (ClassCastException e1) {
  208.                 // Just so clicking and dragging on the window does not
  209.                 // prematurely create a Koch curve.
  210.             }
  211.         });
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement