Skip to content

Commit

Permalink
Completing Chapter 15 exercises;
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryDulaney committed Nov 10, 2023
1 parent bf2cdc6 commit e717f29
Show file tree
Hide file tree
Showing 7 changed files with 633 additions and 0 deletions.
101 changes: 101 additions & 0 deletions ch_15/Exercise15_20.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package ch_15;

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
* **15.20 (Geometry: display angles) Write a program that enables the user to drag the vertices of
* a triangle and displays the angles dynamically as the triangle shape changes,
* as shown in Figure 15.30a. The formula to compute angles is given in Listing 4.1.
* <p>
* FIGURE 15.30 (a) Exercise 15.20 enables the user to drag vertices and display the angles
* dynamically.
*/
public class Exercise15_20 extends Application {
private double radius = 10;
private Circle[] circle = {new Circle(40, 40, 10),
new Circle(140, 40, 10), new Circle(60, 140, 10)};
private Line line1 = new Line();
private Line line2 = new Line();
private Line line3 = new Line();
private Text[] text = {new Text(), new Text(), new Text()};

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
setLines();
pane.getChildren().addAll(circle[0], circle[1], circle[2],
line1, line2, line3, text[0], text[1], text[2]);

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

circle[0].setOnMouseDragged(e -> {
if (circle[0].contains(e.getX(), e.getY())) {
// Recompute and display angles
circle[0].setCenterX(e.getX());
circle[0].setCenterY(e.getY());
setLines();
}
});

circle[1].setOnMouseDragged(e -> {
if (circle[1].contains(e.getX(), e.getY())) {
// Recompute and display angles
circle[1].setCenterX(e.getX());
circle[1].setCenterY(e.getY());
setLines();
}
});

circle[2].setOnMouseDragged(e -> {
if (circle[2].contains(e.getX(), e.getY())) {
// Recompute and display angles
circle[2].setCenterX(e.getX());
circle[2].setCenterY(e.getY());
setLines();
}
});
}

private void setLines() {
line1.setStartX(circle[0].getCenterX());
line1.setStartY(circle[0].getCenterY());
line1.setEndX(circle[1].getCenterX());
line1.setEndY(circle[1].getCenterY());
line2.setStartX(circle[0].getCenterX());
line2.setStartY(circle[0].getCenterY());
line2.setEndX(circle[2].getCenterX());
line2.setEndY(circle[2].getCenterY());
line3.setStartX(circle[1].getCenterX());
line3.setStartY(circle[1].getCenterY());
line3.setEndX(circle[2].getCenterX());
line3.setEndY(circle[2].getCenterY());

// Compute angles
double a = new Point2D(circle[2].getCenterX(), circle[2].getCenterY()).
distance(circle[1].getCenterX(), circle[1].getCenterY());
double b = new Point2D(circle[2].getCenterX(), circle[2].getCenterY()).
distance(circle[0].getCenterX(), circle[0].getCenterY());
double c = new Point2D(circle[1].getCenterX(), circle[1].getCenterY()).
distance(circle[0].getCenterX(), circle[0].getCenterY());
double[] angle = new double[3];
angle[0] = Math.acos((a * a - b * b - c * c) / (-2 * b * c));
angle[1] = Math.acos((b * b - a * a - c * c) / (-2 * a * c));
angle[2] = Math.acos((c * c - b * b - a * a) / (-2 * a * b));

for (int i = 0; i < 3; i++) {
text[i].setX(circle[i].getCenterX());
text[i].setY(circle[i].getCenterY() - radius);
text[i].setText(String.format("%.2f", Math.toDegrees(angle[i])));
}
}
}
79 changes: 79 additions & 0 deletions ch_15/Exercise15_22.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ch_15;


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.Ellipse;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

/**
* 15.22 (Auto resize cylinder) Rewrite Programming Exercise 14.10 so
* that the cylinder’s width and height are automatically resized when the window is resized.
*/
public class Exercise15_22 extends Application {
private double paneWidth = 200;
private double paneHeight = 200;

Ellipse ellipse = new Ellipse(100, 40, 50, 20);
Arc arc1 = new Arc(100, 140, 50, 20, 0, 180);
Arc arc2 = new Arc(100, 140, 50, 20, 180, 180);
Line line1 = new Line(50, 40, 50, 140);
Line line2 = new Line(150, 40, 150, 140);

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();

ellipse.setFill(Color.WHITE);
ellipse.setStroke(Color.BLACK);

arc1.setFill(Color.WHITE);
arc1.setStroke(Color.BLACK);
arc1.getStrokeDashArray().addAll(6d, 21d);

arc2.setFill(Color.WHITE);
arc2.setStroke(Color.BLACK);

pane.getChildren().addAll(ellipse, arc1, arc2, line1, line2);

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

scene.widthProperty().addListener(ov -> {
paneWidth = pane.getWidth();
setValues();
});

scene.heightProperty().addListener(ov -> {
paneHeight = pane.getHeight();
setValues();
});
}

private void setValues() {
ellipse.setCenterX(paneWidth / 2);
ellipse.setRadiusX(paneWidth / 4);

arc1.setCenterX(paneWidth / 2);
arc1.setCenterY(paneHeight - 40);
arc1.setRadiusX(paneWidth / 4);

arc2.setCenterX(paneWidth / 2);
arc2.setCenterY(paneHeight - 40);
arc2.setRadiusX(paneWidth / 4);

line1.setStartX(paneWidth / 4);
line1.setEndX(paneWidth / 4);
line1.setEndY(paneHeight - 40);

line2.setStartX(paneWidth / 2 + paneWidth / 4);
line2.setEndX(paneWidth / 2 + paneWidth / 4);
line2.setEndY(paneHeight - 40);
}
}
52 changes: 52 additions & 0 deletions ch_15/Exercise15_24.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch_15;

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

/**
* **15.24 (Animation: palindrome) Write a program that animates a palindrome swing as
* shown in Figure 15.31. Press/release the mouse to pause/resume the animation.
* <p>
* FIGURE 15.31 The program animates a palindrome swing.
*/
public class Exercise15_24 extends Application {
@Override
public void start(Stage primaryStage) {
Group group = new Group();
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);

PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(4000));
pt.setPath(arc);
pt.setNode(circle);
pt.setOrientation(
PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play(); // Play animation

group.setOnMousePressed(e -> pt.pause());
group.setOnMouseReleased(e -> pt.play());
Scene scene = new Scene(new BorderPane(group), 250, 200);
primaryStage.setTitle(getClass().getName());
primaryStage.setScene(scene);
primaryStage.show();
}
}
60 changes: 60 additions & 0 deletions ch_15/Exercise15_26.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ch_15;

import javafx.animation.FadeTransition;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
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.stage.Stage;
import javafx.util.Duration;

/**
* *15.26 (Change opacity) Rewrite Programming Exercise 15.24 so that the ball’s
* opacity is changed as it swings.
*/
public class Exercise15_26 extends Application {
@Override
public void start(Stage primaryStage) {
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);

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

PathTransition pt = new PathTransition();
pt.setDuration(Duration.millis(4000));
pt.setPath(arc);
pt.setNode(circle);
pt.setOrientation(
PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play(); // Play path transition animation

FadeTransition ft = new FadeTransition(Duration.millis(3000), circle);
ft.setFromValue(1.0);
ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
ft.play(); // Play fade transition animation

pane.setOnMousePressed(e -> pt.pause());
pane.setOnMouseReleased(e -> pt.play());

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

}
Loading

0 comments on commit e717f29

Please sign in to comment.