1
+ /*********************************************************************************
2
+ * (Sierpinski triangle) Revise Listing 18.9 to develop a program that lets the *
3
+ * user use the + and – buttons to increase or decrease the current order by 1, *
4
+ * as shown in Figure 18.12a. The initial order is 0. If the current order is 0, *
5
+ * the Decrease button is ignored. *
6
+ *********************************************************************************/
7
+ import javafx .application .Application ;
8
+ import javafx .geometry .Point2D ;
9
+ import javafx .geometry .Pos ;
10
+ import javafx .scene .Scene ;
11
+ import javafx .scene .control .Button ;
12
+ import javafx .scene .layout .BorderPane ;
13
+ import javafx .scene .layout .HBox ;
14
+ import javafx .scene .layout .Pane ;
15
+ import javafx .scene .paint .Color ;
16
+ import javafx .scene .shape .Polygon ;
17
+ import javafx .stage .Stage ;
18
+
19
+ public class Exercise_18_19 extends Application {
20
+ @ Override // Override the start method in the Application class
21
+ public void start (Stage primaryStage ) {
22
+ SierpinskiTrianglePane trianglePane = new SierpinskiTrianglePane ();
23
+
24
+ // Create to buttons
25
+ Button btDecrease = new Button ("-" );
26
+ Button btIncrease = new Button ("+" );
27
+
28
+ // Create an register the handlers
29
+ btDecrease .setOnAction (e -> {
30
+ if (trianglePane .getOrder () > 0 )
31
+ trianglePane .setOrder (trianglePane .getOrder () - 1 );
32
+ });
33
+
34
+ btIncrease .setOnAction (
35
+ e -> trianglePane .setOrder (trianglePane .getOrder () + 1 ));
36
+
37
+ // Pane to hold label, text field, and a button
38
+ HBox hBox = new HBox (10 );
39
+ hBox .getChildren ().addAll (btDecrease , btIncrease );
40
+ hBox .setAlignment (Pos .CENTER );
41
+
42
+ BorderPane borderPane = new BorderPane ();
43
+ borderPane .setCenter (trianglePane );
44
+ borderPane .setBottom (hBox );
45
+
46
+ // Create a scene and place it in the stage
47
+ Scene scene = new Scene (borderPane , 200 , 210 );
48
+ primaryStage .setTitle ("Exercise_18_19" ); // Set the stage title
49
+ primaryStage .setScene (scene ); // Place the scene in the stage
50
+ primaryStage .show (); // Display the stage
51
+
52
+ scene .widthProperty ().addListener (ov -> trianglePane .paint ());
53
+ scene .heightProperty ().addListener (ov -> trianglePane .paint ());
54
+ }
55
+
56
+ /** Pane for displaying triangles */
57
+ static class SierpinskiTrianglePane extends Pane {
58
+ private int order = 0 ;
59
+
60
+ /** Set a new order */
61
+ public void setOrder (int order ) {
62
+ this .order = order ;
63
+ paint ();
64
+ }
65
+
66
+ /** Return the order */
67
+ public int getOrder () {
68
+ return order ;
69
+ }
70
+
71
+ SierpinskiTrianglePane () {
72
+ }
73
+
74
+ protected void paint () {
75
+ // Select three points in proportion to the pane size
76
+ Point2D p1 = new Point2D (getWidth () / 2 , 10 );
77
+ Point2D p2 = new Point2D (10 , getHeight () - 10 );
78
+ Point2D p3 = new Point2D (getWidth () - 10 , getHeight () - 10 );
79
+
80
+ this .getChildren ().clear (); // Clear the pane before redisplay
81
+
82
+ displayTriangles (order , p1 , p2 , p3 );
83
+ }
84
+
85
+ private void displayTriangles (int order , Point2D p1 ,
86
+ Point2D p2 , Point2D p3 ) {
87
+ if (order == 0 ) {
88
+ // Draw a triangle to connect three points
89
+ Polygon triangle = new Polygon ();
90
+ triangle .getPoints ().addAll (p1 .getX (), p1 .getY (), p2 .getX (),
91
+ p2 .getY (), p3 .getX (), p3 .getY ());
92
+ triangle .setStroke (Color .BLACK );
93
+ triangle .setFill (Color .WHITE );
94
+
95
+ this .getChildren ().add (triangle );
96
+ }
97
+ else {
98
+ //Get the midpoint on each edge in the triangle
99
+ Point2D p12 = p1 .midpoint (p2 );
100
+ Point2D p23 = p2 .midpoint (p3 );
101
+ Point2D p31 = p3 .midpoint (p1 );
102
+
103
+ // Recursively display three triangles
104
+ displayTriangles (order - 1 , p1 , p12 , p31 );
105
+ displayTriangles (order - 1 , p12 , p2 , p23 );
106
+ displayTriangles (order - 1 , p31 , p23 , p3 );
107
+ }
108
+ }
109
+ }
110
+ }
0 commit comments