Skip to content

Commit 4916fc9

Browse files
committed
Add resources for ch 16 + new solutions for chap 16
1 parent 9115e2a commit 4916fc9

Some content is hidden

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

126 files changed

+984
-0
lines changed

ch_16/Exercise16_12.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ch_16;
2+
3+
import javafx.application.Application;
4+
import javafx.geometry.Pos;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.CheckBox;
7+
import javafx.scene.control.ScrollPane;
8+
import javafx.scene.control.TextArea;
9+
import javafx.scene.layout.BorderPane;
10+
import javafx.scene.layout.HBox;
11+
import javafx.stage.Stage;
12+
13+
/**
14+
* *16.12 (Demonstrate TextArea properties) Write a program that demonstrates the
15+
* properties of a text area. The program uses a checkbox to indicate whether the
16+
* text is wrapped onto next line, as shown in Figure 16.41a.
17+
*/
18+
public class Exercise16_12 extends Application {
19+
private final double PANE_WIDTH = 600;
20+
private final double PANE_HEIGHT = 250;
21+
22+
@Override
23+
public void start(Stage primaryStage) {
24+
BorderPane pane = new BorderPane();
25+
TextArea ta = new TextArea();
26+
pane.setCenter(new ScrollPane(ta));
27+
28+
HBox hBox = new HBox(10);
29+
CheckBox editableCheckBox = new CheckBox("Editable");
30+
editableCheckBox.setSelected(true);
31+
ta.setEditable(true);
32+
CheckBox wrapCheckBox = new CheckBox("Wrap");
33+
hBox.getChildren().addAll(editableCheckBox, wrapCheckBox);
34+
wrapCheckBox.setSelected(true);
35+
ta.setWrapText(true);
36+
hBox.setAlignment(Pos.CENTER);
37+
pane.setBottom(hBox);
38+
39+
Scene scene = new Scene(pane, PANE_WIDTH, PANE_HEIGHT);
40+
primaryStage.setTitle(getClass().getName());
41+
primaryStage.setScene(scene);
42+
primaryStage.show();
43+
44+
editableCheckBox.setOnAction(e -> {
45+
ta.setEditable(editableCheckBox.isSelected());
46+
});
47+
48+
wrapCheckBox.setOnAction(e -> {
49+
ta.setWrapText(wrapCheckBox.isSelected());
50+
});
51+
}
52+
53+
}

ch_16/Exercise16_14.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ch_16;
2+
3+
import java.util.List;
4+
5+
import javafx.application.Application;
6+
import javafx.geometry.Pos;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.CheckBox;
9+
import javafx.scene.control.ComboBox;
10+
import javafx.scene.control.Label;
11+
import javafx.scene.layout.BorderPane;
12+
import javafx.scene.layout.HBox;
13+
import javafx.scene.text.Font;
14+
import javafx.scene.text.FontPosture;
15+
import javafx.scene.text.FontWeight;
16+
import javafx.stage.Stage;
17+
18+
/**
19+
* **16.14 (Select a font) Write a program that can dynamically change the font of a text
20+
* in a label displayed on a stack pane. The text can be displayed in bold and
21+
* italic at the same time. You can select the font name or font size from combo
22+
* boxes, as shown in Figure 16.42a. The available font names can be obtained
23+
* using Font.getFamilies(). The combo box for the font size is initialized
24+
* with numbers from 1 to 100.
25+
*/
26+
public class Exercise16_14 extends Application {
27+
private double paneWidth = 680;
28+
private double paneHeight = 250;
29+
private Label label = new Label("Programming is fun");
30+
private ComboBox<String> cboFontName = new ComboBox<>();
31+
private ComboBox<Integer> cboFontSize = new ComboBox<>();
32+
private CheckBox chkBold = new CheckBox("Bold");
33+
private CheckBox chkItalic = new CheckBox("Italic");
34+
35+
@Override
36+
public void start(Stage primaryStage) {
37+
BorderPane pane = new BorderPane();
38+
pane.setCenter(label);
39+
40+
List<String> fontNames = Font.getFontNames();
41+
cboFontName.getItems().addAll(fontNames);
42+
cboFontName.setValue(fontNames.get(0));
43+
for (int i = 1; i <= 100; i++) {
44+
cboFontSize.getItems().add(i);
45+
}
46+
cboFontSize.setValue(20);
47+
48+
HBox hBox = new HBox(10);
49+
hBox.getChildren().addAll(new Label("Font Name"),
50+
cboFontName, new Label("Font Size"), cboFontSize);
51+
hBox.setAlignment(Pos.CENTER);
52+
pane.setTop(hBox);
53+
54+
HBox hBox2 = new HBox(10);
55+
hBox2.getChildren().addAll(chkBold, chkItalic);
56+
hBox2.setAlignment(Pos.CENTER);
57+
pane.setBottom(hBox2);
58+
59+
Scene scene = new Scene(pane, paneWidth, paneHeight);
60+
primaryStage.setTitle(getClass().getName());
61+
primaryStage.setScene(scene);
62+
primaryStage.show();
63+
64+
cboFontName.setOnAction(e -> {
65+
setFont();
66+
});
67+
68+
cboFontSize.setOnAction(e -> {
69+
setFont();
70+
});
71+
72+
chkBold.setOnAction(e -> {
73+
setFont();
74+
});
75+
76+
chkItalic.setOnAction(e -> {
77+
setFont();
78+
});
79+
}
80+
81+
private void setFont() {
82+
FontWeight weight;
83+
if (chkBold.isSelected()) {
84+
weight = FontWeight.BOLD;
85+
} else {
86+
weight = FontWeight.NORMAL;
87+
}
88+
89+
FontPosture posture;
90+
if (chkItalic.isSelected()) {
91+
posture = FontPosture.ITALIC;
92+
} else {
93+
posture = FontPosture.REGULAR;
94+
}
95+
label.setFont(Font.font(cboFontName.getValue(), weight, posture,
96+
cboFontSize.getValue()));
97+
}
98+
}

ch_16/Exercise16_16.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ch_16;
2+
3+
import javafx.application.Application;
4+
import javafx.geometry.Pos;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.ComboBox;
7+
import javafx.scene.control.Label;
8+
import javafx.scene.control.ListView;
9+
import javafx.scene.control.ScrollPane;
10+
import javafx.scene.control.SelectionMode;
11+
import javafx.scene.layout.BorderPane;
12+
import javafx.scene.layout.HBox;
13+
import javafx.stage.Stage;
14+
15+
/**
16+
* 16.16 (Use ComboBox and ListView) Write a program that demonstrates selecting
17+
* items in a list. The program uses a combo box to specify a selection mode, as
18+
* shown in Figure 16.43a. When you select items, they are displayed in a label
19+
* below the list.
20+
*/
21+
public class Exercise16_16 extends Application {
22+
private final double PANE_WIDTH = 280;
23+
private final double PANE_HEIGHT = 250;
24+
private Label label = new Label("No items selected");
25+
private ComboBox<String> cboSelectionMode = new ComboBox<>();
26+
private ListView<String> lv = new ListView();
27+
28+
@Override
29+
public void start(Stage primaryStage) {
30+
BorderPane pane = new BorderPane();
31+
32+
lv.getItems().addAll("China", "Japan", "Korea", "India",
33+
"Malaysia", "Vietnam");
34+
35+
cboSelectionMode.getItems().addAll("SINGLE", "MULTIPLE");
36+
cboSelectionMode.setValue("SINGLE");
37+
38+
HBox hBox = new HBox(10);
39+
hBox.getChildren().addAll(new Label("Choose Selection Mode:"),
40+
cboSelectionMode);
41+
hBox.setAlignment(Pos.CENTER);
42+
pane.setTop(hBox);
43+
pane.setCenter(new ScrollPane(lv));
44+
pane.setBottom(label);
45+
46+
Scene scene = new Scene(pane, PANE_WIDTH, PANE_HEIGHT);
47+
primaryStage.setTitle(getClass().getName());
48+
primaryStage.setScene(scene);
49+
primaryStage.show();
50+
51+
cboSelectionMode.setOnAction(e -> {
52+
if (cboSelectionMode.getValue().equals("SINGLE")) {
53+
lv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
54+
} else {
55+
lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
56+
}
57+
});
58+
59+
lv.getSelectionModel().selectedItemProperty().addListener(ov -> {
60+
StringBuilder items = new StringBuilder();
61+
for (String s : lv.getSelectionModel().getSelectedItems()) {
62+
items.append(s).append(" ");
63+
}
64+
label.setText("Selected items are " + items);
65+
});
66+
}
67+
}

ch_16/Exercise16_18.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package ch_16;
2+
3+
import javafx.animation.KeyFrame;
4+
import javafx.animation.Timeline;
5+
import javafx.application.Application;
6+
import javafx.geometry.Pos;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.Button;
9+
import javafx.scene.control.Slider;
10+
import javafx.scene.layout.BorderPane;
11+
import javafx.scene.layout.HBox;
12+
import javafx.scene.layout.Pane;
13+
import javafx.scene.paint.Color;
14+
import javafx.scene.shape.Arc;
15+
import javafx.scene.shape.ArcType;
16+
import javafx.scene.shape.Circle;
17+
import javafx.stage.Stage;
18+
import javafx.util.Duration;
19+
20+
/**
21+
* **16.18 (Simulation: a running fan) Rewrite Programming Exercise 15.28 to add a slider
22+
* to control the speed of the fan, as shown in Figure 16.43c.
23+
*/
24+
public class Exercise16_18 extends Application {
25+
@Override
26+
public void start(Stage primaryStage) {
27+
CustomFanPane fan = new CustomFanPane();
28+
HBox hBox = new HBox(8);
29+
Button btPause = new Button("Pause");
30+
Button btResume = new Button("Resume");
31+
Button btReverse = new Button("Reverse");
32+
hBox.setAlignment(Pos.CENTER);
33+
hBox.getChildren().addAll(btPause, btResume, btReverse);
34+
35+
BorderPane pane = new BorderPane();
36+
pane.setCenter(fan);
37+
pane.setTop(hBox);
38+
39+
Slider slSpeed = new Slider();
40+
slSpeed.setValue(10);
41+
pane.setBottom(slSpeed);
42+
43+
Scene scene = new Scene(pane, 260, 260);
44+
primaryStage.setTitle(getClass().getName());
45+
primaryStage.setScene(scene);
46+
primaryStage.show();
47+
48+
Timeline animation = new Timeline(
49+
new KeyFrame(Duration.millis(100), e -> fan.move()));
50+
animation.setCycleCount(Timeline.INDEFINITE);
51+
animation.play();
52+
53+
scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));
54+
scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));
55+
56+
btPause.setOnAction(e -> animation.pause());
57+
btResume.setOnAction(e -> animation.play());
58+
btReverse.setOnAction(e -> fan.reverse());
59+
60+
slSpeed.setMax(20);
61+
animation.rateProperty().bind(slSpeed.valueProperty());
62+
}
63+
64+
static class CustomFanPane extends Pane {
65+
private double w = 200;
66+
private double h = 200;
67+
private double radius = Math.min(w, h) * 0.45;
68+
private Arc arc[] = new Arc[4];
69+
private double startAngle = 30;
70+
private Circle circle = new Circle(w / 2, h / 2, radius);
71+
72+
public CustomFanPane() {
73+
circle.setStroke(Color.BLACK);
74+
circle.setFill(Color.WHITE);
75+
getChildren().add(circle);
76+
77+
for (int i = 0; i < 4; i++) {
78+
arc[i] = new Arc(w / 2, h / 2, radius * 0.9,
79+
radius * 0.9, startAngle + i * 90, 35);
80+
arc[i].setFill(Color.RED);
81+
arc[i].setType(ArcType.ROUND);
82+
getChildren().addAll(arc[i]);
83+
}
84+
}
85+
86+
private double increment = 5;
87+
88+
public void reverse() {
89+
increment = -increment;
90+
}
91+
92+
public void move() {
93+
setStartAngle(startAngle + increment);
94+
}
95+
96+
public void setStartAngle(double angle) {
97+
startAngle = angle;
98+
setValues();
99+
}
100+
101+
public void setValues() {
102+
radius = Math.min(w, h) * 0.45;
103+
circle.setRadius(radius);
104+
circle.setCenterX(w / 2);
105+
circle.setCenterY(h / 2);
106+
107+
for (int i = 0; i < 4; i++) {
108+
arc[i].setRadiusX(radius * 0.9);
109+
arc[i].setRadiusY(radius * 0.9);
110+
arc[i].setCenterX(w / 2);
111+
arc[i].setCenterY(h / 2);
112+
arc[i].setStartAngle(startAngle + i * 90);
113+
}
114+
}
115+
116+
public void setW(double w) {
117+
this.w = w;
118+
setValues();
119+
}
120+
121+
public void setH(double h) {
122+
this.h = h;
123+
setValues();
124+
}
125+
}
126+
}
127+
128+

0 commit comments

Comments
 (0)