Skip to content

Commit

Permalink
Merge pull request #78 from HarryDulaney/chapter-15-solutions
Browse files Browse the repository at this point in the history
add 24 and 25
  • Loading branch information
HarryDulaney authored Nov 25, 2023
2 parents 8db4552 + fc175bc commit 05a7945
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 11 deletions.
27 changes: 16 additions & 11 deletions ch_15/Exercise15_24.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
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.scene.shape.Polyline;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

Expand All @@ -21,17 +25,16 @@
public class Exercise15_24 extends Application {
@Override
public void start(Stage primaryStage) {
Group group = new Group();
Pane pane = new Pane();

Circle circle = new Circle(0, 0, 10);
circle.setFill(Color.ORANGE);

Arc arc = new Arc(125, 100, 80, 40, 210, 125);
arc.setFill(Color.WHITE);
arc.setStroke(Color.BLACK);

group.getChildren().add(arc);
group.getChildren().add(circle);

pane.getChildren().add(arc);
pane.getChildren().add(circle);
PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(4000));
pt.setPath(arc);
Expand All @@ -40,13 +43,15 @@ public void start(Stage primaryStage) {
PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play(); // Play animation
pt.play();

group.setOnMousePressed(e -> pt.pause());
group.setOnMouseReleased(e -> pt.play());
Scene scene = new Scene(new BorderPane(group), 250, 200);
pane.setOnMousePressed(e -> pt.pause());
pane.setOnMouseReleased(e -> pt.play());

Scene scene = new Scene(pane, 250, 200);
primaryStage.setTitle(getClass().getName());
primaryStage.setScene(scene);
primaryStage.show();
}
}

121 changes: 121 additions & 0 deletions ch_15/Exercise15_25.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package ch_15;

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
* **15.25 (Animation: ball on curve) Write a program that animates a ball moving along
Expand All @@ -10,8 +21,118 @@
* a click on the left/right mouse button.
*/
public class Exercise15_25 extends Application {
private final double sceneWidth = 600;
private final double sceneHeight = 260;
private final int fontSize = 16;

private final double centerX;
private final double centerY;

public Exercise15_25() {
centerX = sceneWidth / 2;
centerY = sceneHeight / 2;
}

@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
Polyline xAxis = new Polyline();
Polyline yAxis = new Polyline();
drawYAxis(pane, yAxis);
drawXAxis(pane, xAxis);

Polyline polyline1 = new Polyline();
ObservableList<Double> list = polyline1.getPoints();
int startXValue = -170;
int endXValue = 170;
for (int x = startXValue; x <= endXValue; x++) {
list.add(x + centerX);
double y = calculateY(x, centerY);
list.add(y);
if (isYIntersectingXAxis(y, centerY)) {
Text text = new Text(x + centerX, centerY / 0.9, getXAxisLabel(x));
pane.getChildren().add(text);
}

}
Circle point = new Circle(list.get(0), list.get(1), 10);
PathTransition path = new PathTransition(Duration.millis(4000), polyline1, point);
path.setCycleCount(PathTransition.INDEFINITE);
pane.getChildren().addAll(xAxis, yAxis, polyline1, point);
pane.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
path.play();
} else if (e.getButton() == MouseButton.SECONDARY) {
path.pause();
}
});
primaryStage.setScene(new Scene(pane, sceneWidth, sceneHeight));
primaryStage.setTitle(getClass().getName());
primaryStage.show();

}

private double calculateY(int x, double centerY) {
return centerY - 50 * Math.sin((x / 100.0) * 2 * Math.PI);
}

private boolean isYIntersectingXAxis(double y, double centerY) {
return y - centerY < 0.01 && y - centerY > -0.01;
}

private void drawXAxis(Pane pane, Polyline xAxis) {
ObservableList<Double> xAxisList = xAxis.getPoints();
double limit = sceneWidth * 0.95;

for (double x = 0; x < limit; x++) {
xAxisList.add(x);
xAxisList.add(centerY);
}

Line line1 = new Line(limit, centerY, limit - sceneWidth * 0.05, centerY * 0.875);
Line line2 = new Line(limit, centerY, limit - sceneWidth * 0.05, centerY / 0.875);
Text text = new Text(limit + (sceneWidth * 0.02), centerY, "X");
text.setFont(Font.font(fontSize));
pane.getChildren().addAll(line1, line2, text);
}

private void drawYAxis(Pane pane, Polyline yAxis) {

ObservableList<Double> yAxisList = yAxis.getPoints();
double limit = sceneHeight * 0.95;

for (double y = 0; y < limit; y++) {
yAxisList.add(centerX);
yAxisList.add(y + sceneHeight * 0.1);

}
Line line1 = new Line(centerX, sceneHeight * 0.1, centerX - sceneWidth * 0.03, sceneHeight * 0.22);
Line line2 = new Line(centerX, sceneHeight * 0.1, centerX + sceneWidth * 0.03, sceneHeight * 0.22);
Text text = new Text(limit + (sceneWidth * 0.2), sceneHeight * 0.1, "Y");
text.setFont(Font.font(fontSize));
pane.getChildren().addAll(line1, line2, text);
}

private String getXAxisLabel(double x) {
if (isRoughlyEqual(x, 0)) {
return "0";
} else if (isRoughlyEqual(x, 50)) {
return "π";
} else if (isRoughlyEqual(x, -50)) {
return "-π";
} else if (isRoughlyEqual(x, 100)) {
return "2π";
} else if (isRoughlyEqual(x, -100)) {
return "-2π";
}

return "";

}

private boolean isRoughlyEqual(double a, double b) {
return a - b < 0.01 && a - b > -0.01;
}

}

0 comments on commit 05a7945

Please sign in to comment.