Java Reference
In-Depth Information
20 e -> trianglePane.setOrder(Integer.parseInt(tfOrder.getText())));
21 tfOrder.setPrefColumnCount(
4
);
22 tfOrder.setAlignment(Pos.BOTTOM_RIGHT);
23
24
// Pane to hold label, text field, and a button
25 HBox hBox =
new
HBox(
10
);
26 hBox.getChildren().addAll(
new
Label(
"Enter an order: "
), tfOrder);
27 hBox.setAlignment(Pos.CENTER);
28
29 BorderPane borderPane =
new
BorderPane();
30 borderPane.setCenter(trianglePane);
31 borderPane.setBottom(hBox);
32
33
// Create a scene and place it in the stage
34 Scene scene =
new
Scene(borderPane,
200
,
210
);
35 primaryStage.setTitle(
"SierpinskiTriangle"
);
// Set the stage title
36 primaryStage.setScene(scene);
// Place the scene in the stage
37 primaryStage.show();
// Display the stage
38
39 scene.widthProperty().addListener(ov -> trianglePane.paint());
40 scene.heightProperty().addListener(ov -> trianglePane.paint());
41 }
42
43
listener for text field
hold label and text field
listener for resizing
/** Pane for displaying triangles */
44
static class
SierpinskiTrianglePane
extends
Pane {
45
private int
order =
0
;
46
47
/** Set a new order */
48
public void
setOrder(
int
order) {
49
this
.order = order;
50 paint();
51 }
52
53 SierpinskiTrianglePane() {
54 }
55
56
protected void
paint() {
57
// Select three points in proportion to the pane size
58 Point2D p1 =
new
Point2D(getWidth() /
2
,
10
);
59 Point2D p2 =
new
Point2D(
10
, getHeight() -
10
);
60 Point2D p3 =
new
Point2D(getWidth() -
10
, getHeight() -
10
);
61
62
three initial points
this
.getChildren().clear();
// Clear the pane before redisplay
clear the pane
63
64
displayTriangles(order, p1, p2, p3);
draw a triangle
65 }
66
67
private void
displayTriangles(
int
order, Point2D p1,
68 Point2D p2, Point2D p3) {
69
if
(order ==
0
) {
70
// Draw a triangle to connect three points
71 Polygon triangle =
new
Polygon();
72 triangle.getPoints().addAll(p1.getX(), p1.getY(), p2.getX(),
73 p2.getY(), p3.getX(), p3.getY());
74 triangle.setStroke(Color.BLACK);
75 triangle.setFill(Color.WHITE);
76
77
create a triangle
this
.getChildren().add(triangle);
78 }
79
else
{
Search WWH ::
Custom Search