Skip to content

Commit

Permalink
Merge pull request #79 from HarryDulaney/chapter-15-solutions
Browse files Browse the repository at this point in the history
add 27 + 31
  • Loading branch information
HarryDulaney authored Nov 26, 2023
2 parents 05a7945 + 676d224 commit 8a8542a
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Indicates 100% completion of all exercises for that chapter
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_15"><strong>Chapter 15
</strong></a> - Event-Driven Programming and Animations
<h6>
Exercises Needed: 25,27,31,33,35
Exercises Needed: 33,35
</h6>
</li><br>
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_16"><strong>Chapter
Expand Down
36 changes: 36 additions & 0 deletions ch_15/Exercise15_27.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch_15;

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
* *15.27 (Control a moving text) Write a program that displays a moving text, as shown
* in Figure 15.33a and b. The text moves from left to right circularly. When it
* disappears in the right, it reappears from the left. The text freezes when the
* mouse is pressed and moves again when the button is released.
*/
public class Exercise15_27 extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text("Programing is fun");
pane.getChildren().add(text);
PathTransition pt = new PathTransition(Duration.millis(10000),
new Line(-50, 50, 250, 50), text);
pt.setCycleCount(Timeline.INDEFINITE);
pt.play();
pane.setOnMousePressed(e -> pt.pause());
pane.setOnMouseReleased(e -> pt.play());
Scene scene = new Scene(pane, 200, 100);
primaryStage.setTitle("Exercise_15_27");
primaryStage.setScene(scene);
primaryStage.show();
}
}
109 changes: 109 additions & 0 deletions ch_15/Exercise15_31.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package ch_15;

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
* **15.31 (Geometry: pendulum) Write a program that animates a pendulum swinging,
* as shown in Figure 15.35. Press the UP arrow key to faster the speed and the
* DOWN key to slower it. Press the S key to stop animation and the R key to
* resume it.
*/
public class Exercise15_31 extends Application {
private final double sceneWidth = 400;

private final double sceneHeight = 400;

@Override
public void start(Stage primaryStage) throws Exception {
PendulumPane pendulumPane = new PendulumPane(sceneWidth, sceneHeight);
Scene scene = new Scene(pendulumPane, sceneWidth, sceneHeight);
primaryStage.setTitle(getClass().getName());
primaryStage.setScene(scene);
primaryStage.show();

pendulumPane.play();

scene.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP:
pendulumPane.faster();
break;
case DOWN:
pendulumPane.slower();
break;
}
});
}
}

class PendulumPane extends Pane {
PathTransition animationPath;
Circle referenceCircleTop;
Circle referenceCircleBottom;
Line shaft;
Arc pendulumSwingArc;

PendulumPane(double width, double height) {
setPrefWidth(width);
setPrefHeight(height);
pendulumSwingArc = new Arc(width / 2,
height * 0.8,
width * 0.15,
width * 0.15,
180,
180);
pendulumSwingArc.setFill(Color.TRANSPARENT);
pendulumSwingArc.setStroke(Color.BLACK);

referenceCircleBottom = new Circle(pendulumSwingArc.getCenterX() - pendulumSwingArc.getRadiusX(),
pendulumSwingArc.getCenterY(),
10);
referenceCircleTop = new Circle(pendulumSwingArc.getCenterX(),
pendulumSwingArc.getCenterY() - height / 2,
referenceCircleBottom.getRadius() / 2);
pendulumSwingArc = new Arc(referenceCircleTop.getCenterX(),
referenceCircleTop.getCenterY(),
width / 2,
height / 2,
240,
60);
shaft = new Line(
referenceCircleTop.getCenterX(), referenceCircleTop.getCenterY(),
referenceCircleBottom.getCenterX(), referenceCircleBottom.getCenterY());

shaft.endXProperty().bind(referenceCircleBottom.translateXProperty().add(referenceCircleBottom.getCenterX()));
shaft.endYProperty().bind(referenceCircleBottom.translateYProperty().add(referenceCircleBottom.getCenterY()));
animationPath = new PathTransition();
animationPath.setDuration(Duration.millis(4000));
animationPath.setPath(pendulumSwingArc);
animationPath.setNode(referenceCircleBottom);
animationPath.setOrientation(PathTransition.OrientationType.NONE);
animationPath.setCycleCount(PathTransition.INDEFINITE);
animationPath.setAutoReverse(true);

getChildren().addAll(referenceCircleBottom, referenceCircleTop, shaft);

}

public void play() {
animationPath.play();
}


public void faster() {
animationPath.rateProperty().set(animationPath.getRate() + 1);
}

public void slower() {
animationPath.rateProperty().set(animationPath.getRate() - 1);
}
}

0 comments on commit 8a8542a

Please sign in to comment.