Omar_Natour

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

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