Skip to content

Commit 676d224

Browse files
committed
add 27 + 31
1 parent fc175bc commit 676d224

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Indicates 100% completion of all exercises for that chapter
158158
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_15"><strong>Chapter 15
159159
</strong></a> - Event-Driven Programming and Animations
160160
<h6>
161-
Exercises Needed: 25,27,31,33,35
161+
Exercises Needed: 33,35
162162
</h6>
163163
</li><br>
164164
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_16"><strong>Chapter

ch_15/Exercise15_27.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ch_15;
2+
3+
import javafx.animation.PathTransition;
4+
import javafx.animation.Timeline;
5+
import javafx.application.Application;
6+
import javafx.scene.Scene;
7+
import javafx.scene.layout.Pane;
8+
import javafx.scene.shape.Line;
9+
import javafx.scene.text.Text;
10+
import javafx.stage.Stage;
11+
import javafx.util.Duration;
12+
13+
/**
14+
* *15.27 (Control a moving text) Write a program that displays a moving text, as shown
15+
* in Figure 15.33a and b. The text moves from left to right circularly. When it
16+
* disappears in the right, it reappears from the left. The text freezes when the
17+
* mouse is pressed and moves again when the button is released.
18+
*/
19+
public class Exercise15_27 extends Application {
20+
@Override
21+
public void start(Stage primaryStage) {
22+
Pane pane = new Pane();
23+
Text text = new Text("Programing is fun");
24+
pane.getChildren().add(text);
25+
PathTransition pt = new PathTransition(Duration.millis(10000),
26+
new Line(-50, 50, 250, 50), text);
27+
pt.setCycleCount(Timeline.INDEFINITE);
28+
pt.play();
29+
pane.setOnMousePressed(e -> pt.pause());
30+
pane.setOnMouseReleased(e -> pt.play());
31+
Scene scene = new Scene(pane, 200, 100);
32+
primaryStage.setTitle("Exercise_15_27");
33+
primaryStage.setScene(scene);
34+
primaryStage.show();
35+
}
36+
}

ch_15/Exercise15_31.java

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package ch_15;
2+
3+
import javafx.animation.PathTransition;
4+
import javafx.application.Application;
5+
import javafx.scene.Scene;
6+
import javafx.scene.layout.Pane;
7+
import javafx.scene.paint.Color;
8+
import javafx.scene.shape.Arc;
9+
import javafx.scene.shape.Circle;
10+
import javafx.scene.shape.Line;
11+
import javafx.stage.Stage;
12+
import javafx.util.Duration;
13+
14+
/**
15+
* **15.31 (Geometry: pendulum) Write a program that animates a pendulum swinging,
16+
* as shown in Figure 15.35. Press the UP arrow key to faster the speed and the
17+
* DOWN key to slower it. Press the S key to stop animation and the R key to
18+
* resume it.
19+
*/
20+
public class Exercise15_31 extends Application {
21+
private final double sceneWidth = 400;
22+
23+
private final double sceneHeight = 400;
24+
25+
@Override
26+
public void start(Stage primaryStage) throws Exception {
27+
PendulumPane pendulumPane = new PendulumPane(sceneWidth, sceneHeight);
28+
Scene scene = new Scene(pendulumPane, sceneWidth, sceneHeight);
29+
primaryStage.setTitle(getClass().getName());
30+
primaryStage.setScene(scene);
31+
primaryStage.show();
32+
33+
pendulumPane.play();
34+
35+
scene.setOnKeyPressed(e -> {
36+
switch (e.getCode()) {
37+
case UP:
38+
pendulumPane.faster();
39+
break;
40+
case DOWN:
41+
pendulumPane.slower();
42+
break;
43+
}
44+
});
45+
}
46+
}
47+
48+
class PendulumPane extends Pane {
49+
PathTransition animationPath;
50+
Circle referenceCircleTop;
51+
Circle referenceCircleBottom;
52+
Line shaft;
53+
Arc pendulumSwingArc;
54+
55+
PendulumPane(double width, double height) {
56+
setPrefWidth(width);
57+
setPrefHeight(height);
58+
pendulumSwingArc = new Arc(width / 2,
59+
height * 0.8,
60+
width * 0.15,
61+
width * 0.15,
62+
180,
63+
180);
64+
pendulumSwingArc.setFill(Color.TRANSPARENT);
65+
pendulumSwingArc.setStroke(Color.BLACK);
66+
67+
referenceCircleBottom = new Circle(pendulumSwingArc.getCenterX() - pendulumSwingArc.getRadiusX(),
68+
pendulumSwingArc.getCenterY(),
69+
10);
70+
referenceCircleTop = new Circle(pendulumSwingArc.getCenterX(),
71+
pendulumSwingArc.getCenterY() - height / 2,
72+
referenceCircleBottom.getRadius() / 2);
73+
pendulumSwingArc = new Arc(referenceCircleTop.getCenterX(),
74+
referenceCircleTop.getCenterY(),
75+
width / 2,
76+
height / 2,
77+
240,
78+
60);
79+
shaft = new Line(
80+
referenceCircleTop.getCenterX(), referenceCircleTop.getCenterY(),
81+
referenceCircleBottom.getCenterX(), referenceCircleBottom.getCenterY());
82+
83+
shaft.endXProperty().bind(referenceCircleBottom.translateXProperty().add(referenceCircleBottom.getCenterX()));
84+
shaft.endYProperty().bind(referenceCircleBottom.translateYProperty().add(referenceCircleBottom.getCenterY()));
85+
animationPath = new PathTransition();
86+
animationPath.setDuration(Duration.millis(4000));
87+
animationPath.setPath(pendulumSwingArc);
88+
animationPath.setNode(referenceCircleBottom);
89+
animationPath.setOrientation(PathTransition.OrientationType.NONE);
90+
animationPath.setCycleCount(PathTransition.INDEFINITE);
91+
animationPath.setAutoReverse(true);
92+
93+
getChildren().addAll(referenceCircleBottom, referenceCircleTop, shaft);
94+
95+
}
96+
97+
public void play() {
98+
animationPath.play();
99+
}
100+
101+
102+
public void faster() {
103+
animationPath.rateProperty().set(animationPath.getRate() + 1);
104+
}
105+
106+
public void slower() {
107+
animationPath.rateProperty().set(animationPath.getRate() - 1);
108+
}
109+
}

0 commit comments

Comments
 (0)