|
11 | 11 | import javafx.scene.shape.Rectangle;
|
12 | 12 | import javafx.stage.Stage;
|
13 | 13 |
|
| 14 | +/** |
| 15 | + * **32.2 (Visualize data) Write a program that displays the number of students in each |
| 16 | + * department in a pie chart and a bar chart, as shown in Figure 32.27b. The number |
| 17 | + * of students for each department can be obtained from the Student table (see |
| 18 | + * Figure 32.4) using the following SQL statement: |
| 19 | + * select deptId, count(*) |
| 20 | + * from Student |
| 21 | + * where deptId is not null |
| 22 | + * group by deptId; |
| 23 | + */ |
14 | 24 | public class Exercise32_02 extends Application {
|
15 | | - @Override |
16 | | - public void start(Stage primaryStage) { |
17 | | - CarPane car = new CarPane(); |
18 | | - |
19 | | - Scene scene = new Scene(car, 200, 200); |
20 | | - primaryStage.setTitle(getClass().getName()); |
21 | | - primaryStage.setScene(scene); |
22 | | - primaryStage.show(); |
23 | | - |
24 | | - scene.widthProperty().addListener(e -> car.setW(car.getWidth())); |
25 | | - scene.heightProperty().addListener(e -> car.setH(car.getHeight())); |
26 | | - |
27 | | - car.setOnMousePressed(e -> car.suspend()); |
28 | | - car.setOnMouseReleased(e -> car.resume()); |
29 | | - |
30 | | - car.requestFocus(); |
31 | | - car.setOnKeyPressed(e -> { |
32 | | - if (e.getCode() == KeyCode.UP) { |
33 | | - car.faster(); |
34 | | - } |
35 | | - else if (e.getCode() == KeyCode.DOWN) { |
36 | | - car.slower(); |
37 | | - } |
38 | | - }); |
39 | | - } |
| 25 | + @Override |
| 26 | + public void start(Stage primaryStage) { |
| 27 | + CarPane car = new CarPane(); |
| 28 | + |
| 29 | + Scene scene = new Scene(car, 200, 200); |
| 30 | + primaryStage.setTitle(getClass().getName()); |
| 31 | + primaryStage.setScene(scene); |
| 32 | + primaryStage.show(); |
| 33 | + |
| 34 | + scene.widthProperty().addListener(e -> car.setW(car.getWidth())); |
| 35 | + scene.heightProperty().addListener(e -> car.setH(car.getHeight())); |
| 36 | + |
| 37 | + car.setOnMousePressed(e -> car.suspend()); |
| 38 | + car.setOnMouseReleased(e -> car.resume()); |
40 | 39 |
|
41 | | - /** |
42 | | - * The main method is only needed for the IDE with limited |
43 | | - * JavaFX support. Not needed for running from the command line. |
44 | | - */ |
45 | | - public static void main(String[] args) { |
46 | | - launch(args); |
47 | | - } |
48 | | -} |
| 40 | + car.requestFocus(); |
| 41 | + car.setOnKeyPressed(e -> { |
| 42 | + if (e.getCode() == KeyCode.UP) { |
| 43 | + car.faster(); |
| 44 | + } else if (e.getCode() == KeyCode.DOWN) { |
| 45 | + car.slower(); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The main method is only needed for the IDE with limited |
| 52 | + * JavaFX support. Not needed for running from the command line. |
| 53 | + */ |
| 54 | + public static void main(String[] args) { |
| 55 | + launch(args); |
| 56 | + } |
| 57 | +} |
49 | 58 |
|
50 | 59 | class CarPane extends Pane {
|
51 | | - private double w = 200; |
52 | | - private double h = 200; |
53 | | - private double baseX = 0; |
54 | | - private double baseY = h; |
55 | | - private Circle c1 = new Circle(baseX + 10 + 5, baseY - 10 + 5, 5); |
56 | | - private Circle c2 = new Circle(baseX + 30 + 5, baseY - 10 + 5, 5); |
57 | | - |
58 | | - private Rectangle carBody = new Rectangle(baseX, baseY - 20, 50, 10); |
59 | | - private Polygon carTop = new Polygon(baseX + 10, baseY - 20, |
60 | | - baseX + 20, baseY - 30, baseX + 30, baseY - 30, |
61 | | - baseX + 40, baseY - 20); |
62 | | - private int sleepTime = 50; |
63 | | - |
64 | | - private Thread thread = new Thread(() -> { |
65 | | - try { |
66 | | - while (true) { |
67 | | - Platform.runLater(() -> move()); |
68 | | - Thread.sleep(sleepTime); |
69 | | - } |
| 60 | + private double w = 200; |
| 61 | + private double h = 200; |
| 62 | + private double baseX = 0; |
| 63 | + private double baseY = h; |
| 64 | + private Circle c1 = new Circle(baseX + 10 + 5, baseY - 10 + 5, 5); |
| 65 | + private Circle c2 = new Circle(baseX + 30 + 5, baseY - 10 + 5, 5); |
| 66 | + |
| 67 | + private Rectangle carBody = new Rectangle(baseX, baseY - 20, 50, 10); |
| 68 | + private Polygon carTop = new Polygon(baseX + 10, baseY - 20, |
| 69 | + baseX + 20, baseY - 30, baseX + 30, baseY - 30, |
| 70 | + baseX + 40, baseY - 20); |
| 71 | + private int sleepTime = 50; |
| 72 | + |
| 73 | + private Thread thread = new Thread(() -> { |
| 74 | + try { |
| 75 | + while (true) { |
| 76 | + Platform.runLater(() -> move()); |
| 77 | + Thread.sleep(sleepTime); |
| 78 | + } |
| 79 | + } catch (InterruptedException ex) { |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + public CarPane() { |
| 84 | + carBody.setFill(Color.GREEN); |
| 85 | + carTop.setFill(Color.RED); |
| 86 | + this.getChildren().addAll(c1, c2, carBody, carTop); |
| 87 | + |
| 88 | + thread.start(); |
| 89 | + } |
| 90 | + |
| 91 | + public void suspend() { |
| 92 | + thread.suspend(); |
70 | 93 | }
|
71 | | - catch (InterruptedException ex) { |
| 94 | + |
| 95 | + public void resume() { |
| 96 | + thread.resume(); |
| 97 | + } |
| 98 | + |
| 99 | + public void faster() { |
| 100 | + if (sleepTime > 1) |
| 101 | + sleepTime--; |
| 102 | + } |
| 103 | + |
| 104 | + public void slower() { |
| 105 | + sleepTime++; |
| 106 | + } |
| 107 | + |
| 108 | + public void move() { |
| 109 | + if (baseX > w) |
| 110 | + baseX = -20; |
| 111 | + else |
| 112 | + baseX += 1; |
| 113 | + |
| 114 | + setValues(); |
| 115 | + } |
| 116 | + |
| 117 | + public void setValues() { |
| 118 | + c1.setCenterX(baseX + 10 + 5); |
| 119 | + c1.setCenterY(baseY - 10 + 5); |
| 120 | + c2.setCenterX(baseX + 30 + 5); |
| 121 | + c2.setCenterY(baseY - 10 + 5); |
| 122 | + |
| 123 | + carBody.setX(baseX); |
| 124 | + carBody.setY(baseY - 20); |
| 125 | + |
| 126 | + carTop.getPoints().clear(); |
| 127 | + carTop.getPoints().addAll(baseX + 10, baseY - 20, |
| 128 | + baseX + 20, baseY - 30, baseX + 30, baseY - 30, |
| 129 | + baseX + 40, baseY - 20); |
| 130 | + } |
| 131 | + |
| 132 | + public void setW(double w) { |
| 133 | + this.w = w; |
| 134 | + setValues(); |
| 135 | + } |
| 136 | + |
| 137 | + public void setH(double h) { |
| 138 | + this.h = h; |
| 139 | + baseY = h; |
| 140 | + setValues(); |
72 | 141 | }
|
73 | | - }); |
74 | | - |
75 | | - public CarPane() { |
76 | | - carBody.setFill(Color.GREEN); |
77 | | - carTop.setFill(Color.RED); |
78 | | - this.getChildren().addAll(c1, c2, carBody, carTop); |
79 | | - |
80 | | - thread.start(); |
81 | | - } |
82 | | - |
83 | | - public void suspend() { |
84 | | - thread.suspend(); |
85 | | - } |
86 | | - |
87 | | - public void resume() { |
88 | | - thread.resume(); |
89 | | - } |
90 | | - |
91 | | - public void faster() { |
92 | | - if (sleepTime > 1) |
93 | | - sleepTime--; |
94 | | - } |
95 | | - |
96 | | - public void slower() { |
97 | | - sleepTime++; |
98 | | - } |
99 | | - |
100 | | - public void move() { |
101 | | - if (baseX > w) |
102 | | - baseX = -20; |
103 | | - else |
104 | | - baseX += 1; |
105 | | - |
106 | | - setValues(); |
107 | | - } |
108 | | - |
109 | | - public void setValues() { |
110 | | - c1.setCenterX(baseX + 10 + 5); |
111 | | - c1.setCenterY(baseY - 10 + 5); |
112 | | - c2.setCenterX(baseX + 30 + 5); |
113 | | - c2.setCenterY(baseY - 10 + 5); |
114 | | - |
115 | | - carBody.setX(baseX); |
116 | | - carBody.setY(baseY - 20); |
117 | | - |
118 | | - carTop.getPoints().clear(); |
119 | | - carTop.getPoints().addAll(baseX + 10, baseY - 20, |
120 | | - baseX + 20, baseY - 30, baseX + 30, baseY - 30, |
121 | | - baseX + 40, baseY - 20); |
122 | | - } |
123 | | - |
124 | | - public void setW(double w) { |
125 | | - this.w = w; |
126 | | - setValues(); |
127 | | - } |
128 | | - |
129 | | - public void setH(double h) { |
130 | | - this.h = h; |
131 | | - baseY = h; |
132 | | - setValues(); |
133 | | - } |
134 | 142 | }
|
0 commit comments