Skip to content

Commit 86d7208

Browse files
authored
Merge pull request #66 from HarryDulaney/hg-new-solutions
New Solutions for Chapter 15 and 16
2 parents eb7f2fd + 4916fc9 commit 86d7208

File tree

130 files changed

+1356
-86
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1356
-86
lines changed

ch_15/Exercise15_28.java

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class Exercise15_28 extends Application {
2626
@Override
2727
public void start(Stage primaryStage) {
2828
FanPane fan = new FanPane();
29-
3029
HBox hBox = new HBox(5);
3130
Button btPause = new Button("Pause");
3231
Button btResume = new Button("Resume");
@@ -56,66 +55,71 @@ public void start(Stage primaryStage) {
5655
btReverse.setOnAction(e -> fan.reverse());
5756
}
5857

59-
}
60-
61-
class FanPane extends Pane {
62-
private double w = 200;
63-
private double h = 200;
64-
private double radius = Math.min(w, h) * 0.45;
65-
private Arc arc[] = new Arc[4];
66-
private double startAngle = 30;
67-
private Circle circle = new Circle(w / 2, h / 2, radius);
68-
69-
public FanPane() {
70-
circle.setStroke(Color.BLACK);
71-
circle.setFill(Color.WHITE);
72-
getChildren().add(circle);
73-
74-
for (int i = 0; i < 4; i++) {
75-
arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * 90, 35);
76-
arc[i].setFill(Color.RED);
77-
arc[i].setType(ArcType.ROUND);
78-
getChildren().addAll(arc[i]);
58+
static class FanPane extends Pane {
59+
private double w = 200;
60+
private double h = 200;
61+
private double radius = Math.min(w, h) * 0.45;
62+
private Arc arc[] = new Arc[4];
63+
private double startAngle = 30;
64+
private Circle circle = new Circle(w / 2, h / 2, radius);
65+
66+
public FanPane() {
67+
circle.setStroke(Color.BLACK);
68+
circle.setFill(Color.WHITE);
69+
getChildren().add(circle);
70+
71+
for (int i = 0; i < 4; i++) {
72+
arc[i] = new Arc(w / 2,
73+
h / 2,
74+
radius * 0.9,
75+
radius * 0.9,
76+
startAngle + i * 90,
77+
35);
78+
arc[i].setFill(Color.RED);
79+
arc[i].setType(ArcType.ROUND);
80+
getChildren().addAll(arc[i]);
81+
}
7982
}
80-
}
8183

82-
private double increment = 5;
84+
private double increment = 5;
8385

84-
public void reverse() {
85-
increment = -increment;
86-
}
86+
public void reverse() {
87+
increment = -increment;
88+
}
8789

88-
public void move() {
89-
setStartAngle(startAngle + increment);
90-
}
90+
public void move() {
91+
setStartAngle(startAngle + increment);
92+
}
9193

92-
public void setStartAngle(double angle) {
93-
startAngle = angle;
94-
setValues();
95-
}
94+
public void setStartAngle(double angle) {
95+
startAngle = angle;
96+
setValues();
97+
}
9698

97-
public void setValues() {
98-
radius = Math.min(w, h) * 0.45;
99-
circle.setRadius(radius);
100-
circle.setCenterX(w / 2);
101-
circle.setCenterY(h / 2);
102-
103-
for (int i = 0; i < 4; i++) {
104-
arc[i].setRadiusX(radius * 0.9);
105-
arc[i].setRadiusY(radius * 0.9);
106-
arc[i].setCenterX(w / 2);
107-
arc[i].setCenterY(h / 2);
108-
arc[i].setStartAngle(startAngle + i * 90);
99+
public void setValues() {
100+
radius = Math.min(w, h) * 0.45;
101+
circle.setRadius(radius);
102+
circle.setCenterX(w / 2);
103+
circle.setCenterY(h / 2);
104+
105+
for (int i = 0; i < 4; i++) {
106+
arc[i].setRadiusX(radius * 0.9);
107+
arc[i].setRadiusY(radius * 0.9);
108+
arc[i].setCenterX(w / 2);
109+
arc[i].setCenterY(h / 2);
110+
arc[i].setStartAngle(startAngle + i * 90);
111+
}
109112
}
110-
}
111113

112-
public void setW(double w) {
113-
this.w = w;
114-
setValues();
115-
}
114+
public void setW(double w) {
115+
this.w = w;
116+
setValues();
117+
}
116118

117-
public void setH(double h) {
118-
this.h = h;
119-
setValues();
119+
public void setH(double h) {
120+
this.h = h;
121+
setValues();
122+
}
120123
}
124+
121125
}

ch_15/Exercise15_30.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ch_15;
2+
3+
import javafx.animation.Animation;
4+
import javafx.animation.KeyFrame;
5+
import javafx.animation.Timeline;
6+
import javafx.application.Application;
7+
import javafx.scene.Scene;
8+
import javafx.scene.image.Image;
9+
import javafx.scene.image.ImageView;
10+
import javafx.scene.layout.StackPane;
11+
import javafx.stage.Stage;
12+
import javafx.util.Duration;
13+
14+
import java.io.File;
15+
16+
/**
17+
* **15.30 (Slide show) Twenty-five slides are stored as image files (slide0.jpg, slide1
18+
* .jpg, . . . , slide24.jpg) in the image directory downloadable along with the
19+
* source code in the book. The size of each image is 800 * 600.
20+
* <p>
21+
* Write a program that automatically displays the slides repeatedly.
22+
* Each slide is shown for two seconds. The slides are displayed in order. When the last slide finishes, the
23+
* first slide is redisplayed, and so on. Click to pause if the animation is currently
24+
* playing. Click to resume if the animation is currently paused
25+
* <p>
26+
* Note: I used the card images from the book's resources folder instead of the slides as
27+
* I could not find the slides.
28+
*/
29+
public class Exercise15_30 extends Application {
30+
private int index = 0;
31+
32+
@Override
33+
public void start(Stage primaryStage) {
34+
Image[] image = new Image[26];
35+
36+
for (int i = 1; i <= 25; i++) {
37+
String imagePath = "resources" + File.separator + "images" +
38+
File.separator + "card" + File.separator + i + ".png";
39+
image[i] = new Image(imagePath);
40+
}
41+
42+
ImageView imageView = new ImageView(image[0]);
43+
44+
StackPane pane = new StackPane();
45+
pane.getChildren().add(imageView);
46+
Scene scene = new Scene(pane, 800, 600);
47+
primaryStage.setTitle(getClass().getName());
48+
primaryStage.setScene(scene);
49+
primaryStage.show();
50+
51+
Timeline animation = new Timeline(
52+
new KeyFrame(Duration.millis(2000), e -> {
53+
imageView.setImage(image[++index % 25]);
54+
}));
55+
animation.setCycleCount(Timeline.INDEFINITE);
56+
animation.play();
57+
58+
pane.setOnMouseClicked(e -> {
59+
if (animation.getStatus() == Animation.Status.PAUSED) {
60+
animation.play();
61+
} else {
62+
animation.pause();
63+
}
64+
});
65+
}
66+
67+
}

0 commit comments

Comments
 (0)