Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8db4552

Browse files
Merge pull request #77 from HarryDulaney/chapter-15-solutions
Chapter 15 Exer: 25
2 parents 22492e1 + c778dce commit 8db4552

File tree

4 files changed

+119
-4
lines changed

4 files changed

+119
-4
lines changed

‎README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### I've included links below to all the freely accessible companion material and quick links to navigate through my solutions by chapter.
88

9-
### If you would like to contribute, please see: <a href="#contribute">Ways to Contribute</a>
9+
### See <a href="#contribute">Contribution Guide</a> for coding guidelines and information on how to contribute.
1010

1111
____
1212

@@ -41,9 +41,35 @@ object-oriented, GUI programming, advanced GUI and Web programming using Java...
4141
### <a href="https://media.pearsoncmg.com/ph/esm/ecs_liang_ijp_10/supplement/Supplement1dcodingguidelines.html">Java Coding Style Guidelines</a>
4242

4343
____
44+
4445
<span id="contribute"><span/>
46+
## How to Contribute
47+
48+
### _Coding Guidelines_
49+
- #### Solution must use Java 8 SE, as this is the version used by the book.
50+
- #### Every solution should have a java file containing a public main method for testing it.
51+
- #### Naming convention is: ExerciseCC_EE.java where CC is the chapter number and EE is the exercise number.
52+
- #### The public Exercise class containing the main method must include a JavaDoc comment on the class with original exercise question.
53+
- #### Each solution should be its own self-contained program with minimal dependencies on other files. If you need multiple files please create a package for the exercise.
54+
- #### ch_XX/exercise22_07/Exercise22_07.java
55+
- #### This allows us to utilize the Exercise Checking Tool [Exercise Checking Tool](https://liveexample.pearsoncmg.com/CheckExercise/faces/CheckExercise.xhtml?chapter=1&programName=Exercise01_01) to verify solutions.
56+
- ### Example exercise solution:
57+
```java
58+
package ch_01;
59+
/**
60+
* 1.1 (Display three messages) Write a program that displays Welcome to Java,
61+
* Welcome to Computer Science, and Programming is fun.
62+
*/
63+
public class Exercise01_01 {
64+
public static void main(String[] args) {
65+
System.out.println("Welcome to Java");
66+
System.out.println("Welcome to Computer Science");
67+
System.out.println("Programming is fun");
68+
}
69+
70+
}
71+
```
4572

46-
## Contribution Guide:
4773

4874
### <em id="prs">Pull requests:</em>
4975

@@ -132,7 +158,7 @@ Indicates 100% completion of all exercises for that chapter
132158
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_15"><strong>Chapter 15
133159
</strong></a> - Event-Driven Programming and Animations
134160
<h6>
135-
Exercises Needed: 23, 25,27,31,33,35
161+
Exercises Needed: 25,27,31,33,35
136162
</h6>
137163
</li><br>
138164
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/ch_16"><strong>Chapter

‎ch_01/Exercise01_01.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
public class Exercise01_01 {
88
public static void main(String[] args) {
9-
109
System.out.println("Welcome to Java");
1110
System.out.println("Welcome to Computer Science");
1211
System.out.println("Programming is fun");

‎ch_15/Exercise15_23.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package ch_15;
2+
3+
import javafx.application.Application;
4+
import javafx.collections.ObservableList;
5+
import javafx.scene.Scene;
6+
import javafx.scene.layout.StackPane;
7+
import javafx.scene.paint.Color;
8+
import javafx.scene.shape.Polygon;
9+
import javafx.scene.text.Font;
10+
import javafx.scene.text.Text;
11+
import javafx.scene.text.TextAlignment;
12+
import javafx.stage.Stage;
13+
14+
/**
15+
* *15.23 (Auto resize stop sign) Rewrite Programming Exercise 14.15 so that the stop
16+
* sign’s width and height are automatically resized when the window is resized.
17+
*/
18+
public class Exercise15_23 extends Application {
19+
double originalWidth = 200, originalHeight = 200;
20+
double originalFontSize = 42;
21+
@Override
22+
public void start(Stage primaryStage) throws Exception {
23+
StackPane stackPane = new StackPane();
24+
stackPane.setPrefWidth(originalWidth);
25+
stackPane.setPrefHeight(originalHeight);
26+
drawStopSign(stackPane);
27+
Scene scene = new Scene(stackPane, originalWidth, originalHeight);
28+
primaryStage.setScene(scene);
29+
primaryStage.setTitle(getClass().getName());
30+
primaryStage.setResizable(true);
31+
primaryStage.show();
32+
33+
scene.widthProperty().addListener((observable, oldValue, newValue) -> {
34+
stackPane.setPrefWidth((double) newValue);
35+
drawStopSign(stackPane);
36+
37+
});
38+
39+
scene.heightProperty().addListener((observable, oldValue, newValue) -> {
40+
stackPane.setPrefHeight((double) newValue);
41+
drawStopSign(stackPane);
42+
});
43+
}
44+
45+
void drawStopSign(StackPane stackPane) {
46+
Polygon polygon = new Polygon();
47+
stackPane.getChildren().clear();
48+
double width = stackPane.getPrefWidth();
49+
double height = stackPane.getPrefHeight();
50+
stackPane.getChildren().add(polygon);
51+
polygon.setFill(Color.RED);
52+
polygon.setStroke(Color.RED);
53+
54+
ObservableList<Double> list = polygon.getPoints();
55+
double centerX = width / 2, centerY = height / 2;
56+
double radius = Math.min(width, height) * 0.4;
57+
// Add points to the polygon list -> divide Math.PI by 8 for Octagon
58+
for (int i = 0; i < 8; i++) {
59+
list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8));
60+
list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8));
61+
}
62+
polygon.setRotate(22.5);
63+
64+
Text text = new Text("STOP");
65+
text.setTextAlignment(TextAlignment.CENTER);
66+
text.setFill(Color.WHITE);
67+
text.setStroke(Color.WHITE);
68+
text.setFont(Font.font(originalFontSize * Math.min(width, height) / Math.min(originalWidth, originalHeight)));
69+
stackPane.getChildren().add(text);
70+
71+
}
72+
73+
}

‎ch_15/Exercise15_25.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ch_15;
2+
3+
import javafx.application.Application;
4+
import javafx.stage.Stage;
5+
6+
/**
7+
* **15.25 (Animation: ball on curve) Write a program that animates a ball moving along
8+
* a sine curve, as shown in Figure 15.32. When the ball gets to the right border,
9+
* it starts over from the left. Enable the user to resume/pause the animation with
10+
* a click on the left/right mouse button.
11+
*/
12+
public class Exercise15_25 extends Application {
13+
@Override
14+
public void start(Stage primaryStage) throws Exception {
15+
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /