daily pastebin goal
69%
SHARE
TWEET

Recursive Graphics "Tree" Generation Using JavaFX

a guest Nov 5th, 2015 162 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. package chapter18;
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Pos;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.HBox;
  10. import javafx.scene.layout.Pane;
  11. import javafx.scene.shape.Line;
  12. import javafx.stage.Stage;
  13.  
  14. public class GenerateATree extends Application {
  15.        
  16.   @Override // Override the start method in the Application class
  17.   public void start(Stage primaryStage) {      
  18.     TreePane pane = new TreePane();
  19.     TextField tfOrder = new TextField();
  20.     tfOrder.setOnAction(
  21.       e -> pane.setDepth(Integer.parseInt(tfOrder.getText())));
  22.     tfOrder.setPrefColumnCount(4);
  23.     tfOrder.setAlignment(Pos.BOTTOM_RIGHT);
  24.  
  25.     // Pane to hold label, text field, and a button
  26.     HBox hBox = new HBox(10);
  27.     hBox.getChildren().addAll(new Label("Enter an order: "), tfOrder);
  28.     hBox.setAlignment(Pos.CENTER);
  29.    
  30.     BorderPane borderPane = new BorderPane();
  31.     borderPane.setCenter(pane);
  32.     borderPane.setBottom(hBox);
  33.        
  34.     // Create a scene and place it in the stage
  35.     Scene scene = new Scene(borderPane, 200, 210);
  36.     primaryStage.setTitle("Exercise18_39");
  37.     primaryStage.setScene(scene);
  38.     primaryStage.show();
  39.    
  40.     scene.widthProperty().addListener(ov -> pane.paint());
  41.     scene.heightProperty().addListener(ov -> pane.paint());
  42.   }
  43.  
  44.   /** Pane for displaying triangles */
  45.   static class TreePane extends Pane {
  46.     private int depth = 0;
  47.     private double angleFactor = Math.PI / 5;
  48.     private double sizeFactor = 0.58;
  49.    
  50.     private double startX = 0;
  51.     private double startY = 0;
  52.    
  53.     public TreePane() {
  54.       this.setOnMouseDragged(e -> {
  55.           startX = e.getX();
  56.           startY = e.getY() + 40;
  57.           paint();
  58.       });
  59.     }
  60.  
  61.     public void setDepth(int depth) {
  62.       this.depth = depth;
  63.       paint();
  64.     }
  65.  
  66.     public void paint() {
  67.       getChildren().clear();
  68.       startX = getWidth() / 2;
  69.       startY = getHeight();
  70.       paintBranch(depth, startX, startY - 10, getHeight() / 3, Math.PI / 2);
  71.     }
  72.  
  73.     public void paintBranch(int depth, double x1, double y1,
  74.         double length, double angle) {
  75.         if (depth >= 0) {
  76.           double x2 = x1 + Math.cos(angle) * length;
  77.           double y2 = y1 - Math.sin(angle) * length;
  78.  
  79.           // Draw the line
  80.           getChildren().add(new Line(x1, y1, x2, y2));
  81.           // Draw the left branch
  82.           paintBranch(depth - 1, x2, y2, length * sizeFactor, angle + angleFactor);
  83.           // Draw the right branch
  84.           paintBranch(depth - 1, x2, y2, length * sizeFactor, angle - angleFactor);
  85.         }
  86.     }
  87.   }
  88.  
  89.   public static void main(String[] args) {
  90.     launch(args);
  91.   }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top