Chapter 18 Problem 38.PE Textbook: Introduction to Java programming and datastructure. 11th Edition Y. Daniel Liang Publisher: PEARSON ISBN: 9780134670942 This code is taken from your website does not work. package e38; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Textfield; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.stage.Stage; public class E38 extends Application { @Override public void start(Stage primaryStage) { // Create a pane TreePane tp = new TreePane(); // Create a textfield TextField tfOrder = new TextField(); // Set the Depth tfOrder.setOnAction(e->tp.set_Depth(Integer.parseInt(tfOrder.getText()))); // Set the value of the property tfOrder.setPrefColumnCount(4); // Set the alignment tfOrder.setAlignment(Pos.BOTTOM_RIGHT); // Create ahbox pane HBox hBox = new HBox(10); // Add the label and textfield object hBox.getChildren().addAll(new Label("Enter an order"), tfOrder); // Set the alignment hBox.setAlignment(Pos.CENTER); // Create a border pane BorderPane borderPane = new BorderPane(); // Set the pane to center location borderPane.setCenter(tp); // Set the pane at the bottom borderPane.setBottom(hBox); // Create a scene a place it in the stage Scene scene = new Scene(borderPane,200,210); // Set the title primaryStage.setTitle("Exercise18_38"); // Set the scene on the stage primaryStage.setScene(scene); // Display the stage primaryStage.show(); // Set the width scene.widthProperty().addListener(ov->tp.paint()); // Set the height scene.heightProperty().addListener(ov->tp.paint()); } // Pane for displaying Tree static class TreePane extends Pane { // Declare required variables private int treedepth = 0; private double angle_Factor =Math.PI/5; private double size_Factor =0.58; // Function to set depth of the tree public void set_Depth(int treedepth) { // Seth the depth this.treedepth = treedepth; // Call the function paint(); } // Function definition to paint public void paint() { // Clear getChildren().clear(); // Paint the branch paint_Branch(treedepth,getWidth()/2,getHeight()-10,getHeight()/3, Math.PI/2); } // Function definition to paint the branch public void paint_Branch(int treedepth, double x1, double y1,double length, double angle) { // Check if the tree depth is greater than 0 if(treedepth>=0) { // Calculate the values double x2 = x1 + Math.cos(angle)*length; double y2 = y1 + Math.sin(angle)*length; // Draw the line and add it to the pane getChildren().add(new Line(x1,y1,x2,y2)); // Draw the left branch paint_Branch(treedepth-1, x2,y2,length*size_Factor, angle+angle_Factor); // Draw the right branch paint_Branch(treedepth-1, x2,y2,length*size_Factor, angle-angle_Factor); } } } public static void main(String[]args) { launch(args); } }
Chapter 18 Problem 38.PE Textbook: Introduction to Java programming and datastructure.
This code is taken from your website does not work.
package e38;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Textfield;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class E38 extends Application {
@Override
public void start(Stage primaryStage) {
// Create a pane
TreePane tp = new TreePane();
// Create a textfield
TextField tfOrder = new TextField();
// Set the Depth
tfOrder.setOnAction(e->tp.set_Depth(Integer.parseInt(tfOrder.getText())));
// Set the value of the property
tfOrder.setPrefColumnCount(4);
// Set the alignment
tfOrder.setAlignment(Pos.BOTTOM_RIGHT);
// Create ahbox pane
HBox hBox = new HBox(10);
// Add the label and textfield object
hBox.getChildren().addAll(new Label("Enter an order"), tfOrder);
// Set the alignment
hBox.setAlignment(Pos.CENTER);
// Create a border pane
BorderPane borderPane = new BorderPane();
// Set the pane to center location
borderPane.setCenter(tp);
// Set the pane at the bottom
borderPane.setBottom(hBox);
// Create a scene a place it in the stage
Scene scene = new Scene(borderPane,200,210);
// Set the title
primaryStage.setTitle("Exercise18_38");
// Set the scene on the stage
primaryStage.setScene(scene);
// Display the stage
primaryStage.show();
// Set the width
scene.widthProperty().addListener(ov->tp.paint());
// Set the height
scene.heightProperty().addListener(ov->tp.paint());
}
// Pane for displaying Tree
static class TreePane extends Pane
{
// Declare required variables
private int treedepth = 0;
private double angle_Factor =Math.PI/5;
private double size_Factor =0.58;
// Function to set depth of the tree
public void set_Depth(int treedepth)
{
// Seth the depth
this.treedepth = treedepth;
// Call the function
paint();
}
// Function definition to paint
public void paint()
{
// Clear
getChildren().clear();
// Paint the branch
paint_Branch(treedepth,getWidth()/2,getHeight()-10,getHeight()/3, Math.PI/2);
}
// Function definition to paint the branch
public void paint_Branch(int treedepth, double x1, double y1,double length, double angle)
{
// Check if the tree depth is greater than 0
if(treedepth>=0)
{
// Calculate the values
double x2 = x1 + Math.cos(angle)*length;
double y2 = y1 + Math.sin(angle)*length;
// Draw the line and add it to the pane
getChildren().add(new Line(x1,y1,x2,y2));
// Draw the left branch
paint_Branch(treedepth-1, x2,y2,length*size_Factor, angle+angle_Factor);
// Draw the right branch
paint_Branch(treedepth-1, x2,y2,length*size_Factor, angle-angle_Factor);
}
}
}
public static void main(String[]args)
{
launch(args);
}
}
Step by step
Solved in 2 steps