From b96297eca5e7dfd1dcca14f3a4d853f4b4117330 Mon Sep 17 00:00:00 2001 From: Harry Dulaney Date: Sun, 19 Nov 2023 09:18:02 -0500 Subject: [PATCH] update readme + update ex. 30 02 and 15 29 --- README.md | 135 ++++++++++++----- ch_15/Exercise15_29.java | 192 +++++++++++------------- ch_30/Exercise30_02.java | 133 ++++++++++++++-- resources/images/icons8-complete-26.png | Bin 0 -> 655 bytes 4 files changed, 304 insertions(+), 156 deletions(-) create mode 100644 resources/images/icons8-complete-26.png diff --git a/README.md b/README.md index 8618076..e6418ce 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,6 @@ ____ ## Contribution Guide: - ### Pull requests: - _When to use Pull Requests_ @@ -62,62 +61,126 @@ ____ - To request a change to an existing solution. - You find an error in an existing solution. - ____

Exercise Solutions:

-
Quick Links to navigate these solutions by chapter
+
Quick Links to navigate these solutions by chapter
+
+complete-check-img +Indicates 100% completion of all exercises for that chapter +
____ diff --git a/ch_15/Exercise15_29.java b/ch_15/Exercise15_29.java index 2266472..41af150 100644 --- a/ch_15/Exercise15_29.java +++ b/ch_15/Exercise15_29.java @@ -6,6 +6,7 @@ import javafx.scene.Scene; import javafx.scene.input.KeyCode; import javafx.scene.layout.Pane; +import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; @@ -23,141 +24,120 @@ */ public class Exercise15_29 extends Application { - private final double WIDTH = 600; - private final double HEIGHT = 100; - @Override public void start(Stage primaryStage) throws Exception { - RacingPane pane = new RacingPane(WIDTH, HEIGHT); - pane.start(); - pane.setOnMousePressed(e -> pane.pause()); - pane.setOnMouseReleased(e -> pane.play()); + CarPane car = new CarPane(); - pane.setOnKeyPressed(e -> { - if (e.getCode() == KeyCode.UP) { - pane.increaseSpeed(); - } else if (e.getCode() == KeyCode.DOWN) { - pane.decreaseSpeed(); - } - }); - - Scene scene = new Scene(pane, WIDTH, HEIGHT); + Scene scene = new Scene(car, 200, 200); primaryStage.setTitle(getClass().getName()); primaryStage.setScene(scene); primaryStage.show(); - pane.requestFocus(); - } - -} - -class RacingPane extends Pane { - private double x; - private double y; - private CarShape car; - private Timeline animation; - - /** - * Construct and animate a default CarPane - */ - RacingPane(double width, double height) { - setHeight(height); - setWidth(width); - car = new CarShape(x, y, 5, this); - x = 0; - y = 100; - animation = new Timeline( - new KeyFrame(Duration.millis(50), e -> car.moveCar(x, y, this))); - animation.setCycleCount(Timeline.INDEFINITE); - } + scene.widthProperty().addListener(e -> car.setW(car.getWidth())); + scene.heightProperty().addListener(e -> car.setH(car.getHeight())); + car.setOnMousePressed(e -> car.pause()); + car.setOnMouseReleased(e -> car.play()); - void start() { - animation.play(); - } - - - /** - * Pause animation - */ - public void pause() { - animation.pause(); - } - - /** - * Play animation - */ - public void play() { - animation.play(); - } - - /** - * Increase rate by 1 - */ - public void increaseSpeed() { - animation.setRate(animation.getRate() + 1); + car.requestFocus(); + car.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.UP) { + car.increaseSpeed(); + } else if (e.getCode() == KeyCode.DOWN) { + car.decreaseSpeed(); + } + }); + car.play(); } - /** - * decrease rate by 1 - */ - public void decreaseSpeed() { - animation.setRate(animation.getRate() > 0 ? animation.getRate() - 1 : 0); - } - class CarShape { - private double wheelRadius; - private Rectangle lowerBody; - private Polygon topBody; - private Circle wheel1; - private Circle wheel2; + class CarPane extends Pane { + private double w = 200; + private double h = 200; + private double baseX = 0; + private double baseY = h; + private Circle c1 = new Circle(baseX + 10 + 5, baseY - 10 + 5, 5); + private Circle c2 = new Circle(baseX + 30 + 5, baseY - 10 + 5, 5); + private double animationRate = 0.8; + + private Rectangle carBody = new Rectangle(baseX, baseY - 20, 50, 10); + private Polygon carTop = new Polygon(baseX + 10, baseY - 20, + baseX + 20, baseY - 30, baseX + 30, baseY - 30, + baseX + 40, baseY - 20); + private Timeline animation; + + public CarPane() { + carBody.setFill(Color.BLACK); + carTop.setFill(Color.BLACK); + animation = new Timeline( + new KeyFrame(Duration.millis(50), e -> move())); + animation.setCycleCount(Timeline.INDEFINITE); + animation.setRate(animationRate); + this.getChildren().addAll(c1, c2, carBody, carTop); + move(); + } - public CarShape(double x, double y, double wheelRadius, Pane pane) { - this.wheelRadius = wheelRadius; - drawAndRender(x, y, pane); + /** + * Pause animation + */ + public void pause() { + animation.pause(); } - void drawAndRender(double x, double y, Pane pane) { - if (!pane.getChildren().isEmpty()) { - pane.getChildren().clear(); - } + /** + * Play animation + */ + public void play() { + animation.play(); + } - lowerBody = new Rectangle(x, y - 20, 50, 10); - topBody = new Polygon(x + 10, y - 20, x + 20, y - 30, x + 30, - y - 30, x + 40, y - 20); - wheel1 = new Circle(x + 15, y - 5, wheelRadius); - wheel2 = new Circle(x + 35, y - 5, wheelRadius); - pane.getChildren().addAll(getLowerBody(), getWheel1(), getWheel2(), getTopBody()); + /** + * Increase rate by 1 + */ + public void increaseSpeed() { + animation.setRate(animation.getRate() + 0.01); } /** - * Calculate new x position and draw car - * - * @param x the current x position - * @param y the current y position - * @param pane the pane to render the car on + * decrease rate by 1 */ - protected void moveCar(double x, double y, Pane pane) { - x += 1; + public void decreaseSpeed() { + animation.setRate(animation.getRate() > 0.01 ? animation.getRate() - 0.01 : 0.01); + } + public void move() { + if (baseX > w) + baseX = -20; + else + baseX += 1; - drawAndRender(x, y, pane); + setValues(); } - public Rectangle getLowerBody() { - return lowerBody; - } + public void setValues() { + c1.setCenterX(baseX + 10 + 5); + c1.setCenterY(baseY - 10 + 5); + c2.setCenterX(baseX + 30 + 5); + c2.setCenterY(baseY - 10 + 5); + + carBody.setX(baseX); + carBody.setY(baseY - 20); - public Polygon getTopBody() { - return topBody; + carTop.getPoints().clear(); + carTop.getPoints().addAll(baseX + 10, baseY - 20, + baseX + 20, baseY - 30, baseX + 30, baseY - 30, + baseX + 40, baseY - 20); } - public Circle getWheel1() { - return wheel1; + public void setW(double w) { + this.w = w; + setValues(); } - public Circle getWheel2() { - return wheel2; + public void setH(double h) { + this.h = h; + baseY = h; + setValues(); } } } \ No newline at end of file diff --git a/ch_30/Exercise30_02.java b/ch_30/Exercise30_02.java index f147104..f7adcc3 100644 --- a/ch_30/Exercise30_02.java +++ b/ch_30/Exercise30_02.java @@ -1,5 +1,16 @@ package ch_30; +import javafx.application.Application; +import javafx.application.Platform; +import javafx.scene.Scene; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.Pane; +import javafx.scene.paint.Color; +import javafx.scene.shape.Circle; +import javafx.scene.shape.Polygon; +import javafx.scene.shape.Rectangle; +import javafx.stage.Stage; + import java.util.*; import java.util.stream.Collectors; @@ -8,21 +19,115 @@ * car racing. Compare the program with Programming Exercise 15.29 by setting * the delay time to 10 in both programs. Which one runs the animation faster? */ -public class Exercise30_02 { - public static void main(String[] args) { - Scanner input = new Scanner(System.in); - ArrayList list = new ArrayList<>(); - - System.out.print("Enter the integers between 1 and 100: "); - int number = input.nextInt(); // number read from a file - while (number != 0) { - if (number <= 100 && number >= 0) - list.add(number); - number = input.nextInt(); +public class Exercise30_02 extends Application { + @Override + public void start(Stage primaryStage) { + CarPane car = new CarPane(); + + Scene scene = new Scene(car, 200, 200); + primaryStage.setTitle(getClass().getName()); + primaryStage.setScene(scene); + primaryStage.show(); + + scene.widthProperty().addListener(e -> car.setW(car.getWidth())); + scene.heightProperty().addListener(e -> car.setH(car.getHeight())); + + car.setOnMousePressed(e -> car.suspend()); + car.setOnMouseReleased(e -> car.resume()); + + car.requestFocus(); + car.setOnKeyPressed(e -> { + if (e.getCode() == KeyCode.UP) { + car.faster(); + } else if (e.getCode() == KeyCode.DOWN) { + car.slower(); + } + }); + } + +} + +class CarPane extends Pane { + private double w = 200; + private double h = 200; + private double baseX = 0; + private double baseY = h; + private Circle c1 = new Circle(baseX + 10 + 5, baseY - 10 + 5, 5); + private Circle c2 = new Circle(baseX + 30 + 5, baseY - 10 + 5, 5); + + private Rectangle carBody = new Rectangle(baseX, baseY - 20, 50, 10); + private Polygon carTop = new Polygon(baseX + 10, baseY - 20, + baseX + 20, baseY - 30, baseX + 30, baseY - 30, + baseX + 40, baseY - 20); + private int sleepTime = 50; + + private Thread thread = new Thread(() -> { + try { + while (true) { + Platform.runLater(() -> move()); + Thread.sleep(sleepTime); + } + } catch (InterruptedException ex) { } + }); + + public CarPane() { + carBody.setFill(Color.GREEN); + carTop.setFill(Color.RED); + this.getChildren().addAll(c1, c2, carBody, carTop); + + thread.start(); + } + + public void suspend() { + thread.suspend(); + } + + public void resume() { + thread.resume(); + } + + public void faster() { + if (sleepTime > 1) + sleepTime--; + } + + public void slower() { + sleepTime++; + } + + public void move() { + if (baseX > w) + baseX = -20; + else + baseX += 1; + + setValues(); + } + + public void setValues() { + c1.setCenterX(baseX + 10 + 5); + c1.setCenterY(baseY - 10 + 5); + c2.setCenterX(baseX + 30 + 5); + c2.setCenterY(baseY - 10 + 5); + + carBody.setX(baseX); + carBody.setY(baseY - 20); + + carTop.getPoints().clear(); + carTop.getPoints().addAll(baseX + 10, baseY - 20, + baseX + 20, baseY - 30, baseX + 30, baseY - 30, + baseX + 40, baseY - 20); + } + + public void setW(double w) { + this.w = w; + setValues(); + } - list.stream().collect(Collectors.groupingBy(e -> e, - TreeMap::new, Collectors.counting())).forEach((k, v) -> - System.out.println(k + " occurs " + v + (v == 1 ? " time " : " times"))); + public void setH(double h) { + this.h = h; + baseY = h; + setValues(); } } diff --git a/resources/images/icons8-complete-26.png b/resources/images/icons8-complete-26.png new file mode 100644 index 0000000000000000000000000000000000000000..f01db1eb4409a7f45264d0c261321286a9bf49ce GIT binary patch literal 655 zcmV;A0&x9_P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D0whU9K~zXf?UT<- zR8bVi&$*+`5G|@r3W)^8NJ$F~goGe{Pg|HEMG+x5D6|QJFz6p>SBpRh(dJncwU!`k zV#5K&DP{et9ME!+D@yWnL&MgVzQ z3=!1?l9^aVw-5?s#gZA!j$~fD%@X}}>%8=_HbCwo6v$FFh^SDJf7_24tqOCG{L4TWN2RR5YqFv|Eu2-e)DpsK)ZIln}6|g4~vE2E?(}NYOb(JtpBJ6-= z@Z)PiM6qb4)}ei0P+k)5WTqnxtiLuNKk)YR>__(k8GOr$JstRKQ%Jkv1#9V2s1uK$ z4?n635&dx)j?Q>Hl#NZ`>1m`PetjX#$Ag{PSa*e)ZsNJj