package com.example.demo;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Sierpinski extends Application {
@Override
public void start(Stage primaryStage) {
SierpinskiTrianglePane trianglePane = new SierpinskiTrianglePane();
TextField tfOrder = new TextField();
tfOrder.setOnAction(e -> trianglePane.setOrder(Integer.parseInt(tfOrder.getText())));
tfOrder.setPrefColumnCount(4);
tfOrder.setAlignment(Pos.BOTTOM_RIGHT);
HBox hBox = new HBox(10);
hBox.getChildren().addAll(new Label("Enter an order"), tfOrder);
hBox.setAlignment(Pos.CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(trianglePane);
borderPane.setBottom(hBox);
Scene scene = new Scene(borderPane, 200, 210);
primaryStage.setTitle("SierpinskiTriangle");
primaryStage.setScene(scene);
primaryStage.show();
scene.widthProperty().addListener(ov -> trianglePane.paint());
scene.heightProperty().addListener(ov -> trianglePane.paint());
}
}
I got the solution from Daniel Liang's java textbook. I understand the solution, but I would not be able to create it on my own.
Before you try to make it OOP style, you should define what parts you would like to reuse and what parts will be used just once. For example, you may always have just a single scene.
You may have a lot of triangles, so your triangle can be a class with properties for starting position and number of recursions. That is just an example, and you can have many variations!