Created
April 11, 2022 09:27
-
-
Save Da9el00/bfa88a9b24d5baba6912246283894305 to your computer and use it in GitHub Desktop.
JavaFX - Sierpinski Triangle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Platform; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.layout.BorderPane; | |
import java.net.URL; | |
import java.time.LocalTime; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
@FXML | |
private Label time; | |
@FXML | |
private TextField textInput; | |
@FXML | |
private BorderPane borderPane; | |
SierpinskiTrianglePane trianglePane = new SierpinskiTrianglePane(); | |
@FXML | |
private Label timeLabel; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
borderPane.setCenter(trianglePane); | |
trianglePane.widthProperty().addListener(ov -> trianglePane.paint()); | |
trianglePane.heightProperty().addListener(ov -> trianglePane.paint()); | |
trianglePane.paint(); | |
textInput.setOnAction( | |
e -> trianglePane.setOrder(Integer.parseInt(textInput.getText()))); | |
//timeHandler = new TimeHandler(time); | |
Runnable timeHandler = new Runnable() { | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
Thread.sleep(1000); | |
Platform.runLater(new Runnable() { | |
@Override | |
public void run() { | |
timeLabel.setText(String.valueOf(LocalTime.now())); | |
} | |
}); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}; | |
Thread thread = new Thread(timeHandler); | |
thread.start(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.BorderPane?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #EAF4D3;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<TextField fx:id="textInput" layoutX="226.0" layoutY="332.0" /> | |
<BorderPane fx:id="borderPane" layoutX="150.0" layoutY="33.0" prefHeight="260.0" prefWidth="301.0" /> | |
<Label fx:id="time" layoutX="64.0" layoutY="192.0" text="Label" /> | |
</children> | |
</AnchorPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.geometry.Point2D; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Polygon; | |
public class SierpinskiTrianglePane extends Pane { | |
private int order = 0; | |
public void setOrder(int order) { | |
this.order = order; | |
paint(); | |
} | |
SierpinskiTrianglePane() { | |
} | |
protected void paint() { | |
// Select three points in proportion to the pane size | |
Point2D p1 = new Point2D(getWidth() / 2, 10); | |
Point2D p2 = new Point2D(10, getHeight() - 10); | |
Point2D p3 = new Point2D(getWidth() - 10, getHeight() - 10); | |
this.getChildren().clear(); // Clear the pane before redisplay | |
displayTriangles(order, p1, p2, p3); | |
} | |
private void displayTriangles(int order, Point2D p1, | |
Point2D p2, Point2D p3) { | |
if (order == 0) { | |
// Draw a triangle to connect three points | |
Polygon triangle = new Polygon(); | |
triangle.getPoints().addAll(p1.getX(), p1.getY(), p2.getX(), | |
p2.getY(), p3.getX(), p3.getY()); | |
triangle.setStroke(Color.BLACK); | |
triangle.setFill(Color.WHITE); | |
this.getChildren().add(triangle); | |
} else { | |
// Get the midpoint on each edge in the triangle | |
Point2D p12 = p1.midpoint(p2); | |
Point2D p23 = p2.midpoint(p3); | |
Point2D p31 = p3.midpoint(p1); | |
// Recursively display three triangles | |
displayTriangles(order - 1, p1, p12, p31); | |
displayTriangles(order - 1, p12, p2, p23); | |
displayTriangles(order - 1, p31, p23, p3); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment