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 13add97

Browse files
Merge pull request #61 from HarryDulaney/hgd4-ch-15
Add ch 15 solutions for ex 17, 18, 19.
2 parents 5a9d01f + 6da47c5 commit 13add97

File tree

4 files changed

+267
-2
lines changed

4 files changed

+267
-2
lines changed

‎ch_09/exercise09_06/StopWatch.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public StopWatch() {
4848
/**
4949
* しかく A method named start() that resets the startTime to the current time.
5050
*/
51-
void start() {
51+
publicvoid start() {
5252
startTime = System.currentTimeMillis();
5353
}
5454

5555
/**
5656
* しかく A method named stop() that sets the endTime to the current time.
5757
*/
58-
void stop() {
58+
publicvoid stop() {
5959
endTime = System.currentTimeMillis();
6060
}
6161

‎ch_15/Exercise15_17.java‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package ch_15;
2+
3+
import javafx.application.Application;
4+
import javafx.collections.ObservableList;
5+
import javafx.geometry.Insets;
6+
import javafx.scene.Node;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.Label;
9+
import javafx.scene.input.MouseButton;
10+
import javafx.scene.layout.Pane;
11+
import javafx.scene.layout.VBox;
12+
import javafx.scene.paint.Color;
13+
import javafx.scene.shape.Circle;
14+
import javafx.scene.shape.Rectangle;
15+
import javafx.stage.Stage;
16+
17+
import java.util.ArrayList;
18+
19+
/**
20+
* **15.17 (Geometry: find the bounding rectangle) Write a program that enables the user
21+
* to add and remove points in a two-dimensional plane dynamically, as shown
22+
* in Figure 15.29a. A minimum bounding rectangle is updated as the points are
23+
* added and removed. Assume that the radius of each point is 10 pixels
24+
*/
25+
public class Exercise15_17 extends javafx.application.Application {
26+
27+
Pane pane = new Pane();
28+
ArrayList<Circle> points = new ArrayList<>();
29+
static final int RADIUS = 10;
30+
static final Color FILL_COLOR = Color.TRANSPARENT;
31+
static final Color LINE_COLOR = Color.BLACK;
32+
33+
static final Label instructionsHeader = new Label("INSTRUCTION");
34+
static final Label addInstruction = new Label("Add: Left Click");
35+
static final Label removeInstruction = new Label("Remove: Right Click");
36+
37+
public static void main(String[] args) {
38+
Application.launch(args);
39+
}
40+
41+
@Override
42+
public void start(Stage primaryStage) {
43+
double width = 600;
44+
double height = 400;
45+
VBox instructionsBox = new VBox();
46+
instructionsBox.setPadding(new Insets(5, 5, 5, 5));
47+
instructionsBox.setStyle("-fx-border-color: black");
48+
49+
50+
instructionsBox.getChildren().addAll(instructionsHeader, addInstruction, removeInstruction);
51+
Pane infoPane = new Pane(instructionsBox);
52+
infoPane.setPadding(new Insets(10, 10, 10, 10));
53+
54+
pane.getChildren().addAll(infoPane);
55+
instructionsBox.setLayoutX(10);
56+
instructionsBox.setLayoutY(10);
57+
58+
pane.setOnMouseClicked(e -> {
59+
double x = e.getX();
60+
double y = e.getY();
61+
if (infoPane.contains(x, y)) return;
62+
63+
if (e.getButton() == MouseButton.PRIMARY) {
64+
Circle c = drawPoint(x, y);
65+
points.add(c);
66+
pane.getChildren().add(c);
67+
drawMinBoundingRec();
68+
69+
} else if (e.getButton() == MouseButton.SECONDARY) {
70+
removePoint(x, y);
71+
drawMinBoundingRec();
72+
73+
}
74+
75+
});
76+
77+
Scene scene = new Scene(pane, width, height);
78+
primaryStage.setScene(scene);
79+
primaryStage.setTitle("click to draw circle");
80+
primaryStage.show();
81+
}
82+
83+
private Circle drawPoint(double x, double y) {
84+
Circle c = new Circle(x, y, RADIUS, FILL_COLOR);
85+
c.setStroke(LINE_COLOR);
86+
return c;
87+
}
88+
89+
private void drawMinBoundingRec() {
90+
clearCurrentRectangle();
91+
92+
if (points.isEmpty()) return;
93+
94+
Circle top = points.get(0);
95+
Circle bottom = points.get(0);
96+
Circle right = points.get(0);
97+
Circle left = points.get(0);
98+
99+
for (Circle c : points) {
100+
if (c.getCenterX() < left.getCenterX()) left = c;
101+
if (c.getCenterX() > right.getCenterX()) right = c;
102+
if (c.getCenterY() > bottom.getCenterY()) bottom = c;
103+
if (c.getCenterY() < top.getCenterY()) top = c;
104+
}
105+
double width = right.getCenterX() - left.getCenterX() + top.getRadius() * 2;
106+
double height = bottom.getCenterY() - top.getCenterY() + top.getRadius() * 2;
107+
double centerX = (right.getCenterX() + left.getCenterX()) / 2;
108+
double centerY = (top.getCenterY() + bottom.getCenterY()) / 2;
109+
110+
Rectangle rec = calculateRectangle(centerX, centerY, width, height);
111+
pane.getChildren().add(rec);
112+
113+
}
114+
115+
Rectangle calculateRectangle(double centerX, double centerY, double width, double height) {
116+
Rectangle rec = new Rectangle(centerX - width / 2, centerY - height / 2, width, height);
117+
rec.setStroke(LINE_COLOR);
118+
rec.setFill(FILL_COLOR);
119+
return rec;
120+
}
121+
122+
123+
private void removePoint(double x, double y) {
124+
ObservableList<Node> list = pane.getChildren();
125+
for (int i = list.size() - 1; i >= 0; i--) {
126+
Node c = list.get(i);
127+
128+
if (c instanceof Circle && c.contains(x, y)) {
129+
pane.getChildren().remove(c);
130+
points.remove(c);
131+
132+
break;
133+
}
134+
}
135+
}
136+
137+
private void clearCurrentRectangle() {
138+
ObservableList<Node> list = pane.getChildren();
139+
140+
for (Node c : list) {
141+
if (c instanceof Rectangle) {
142+
pane.getChildren().remove(c);
143+
144+
break;
145+
}
146+
}
147+
148+
}
149+
}

‎ch_15/Exercise15_18.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ch_15;
2+
3+
import javafx.application.Application;
4+
import javafx.scene.Scene;
5+
import javafx.scene.layout.Pane;
6+
import javafx.scene.paint.Color;
7+
import javafx.scene.shape.Rectangle;
8+
import javafx.stage.Stage;
9+
10+
/**
11+
* **15.18 (Move a rectangle using mouse) Write a program that displays a rectangle.
12+
* You can point the mouse inside the rectangle and drag (i.e., move with mouse
13+
* pressed) the rectangle wherever the mouse goes. The mouse point becomes the
14+
* center of the rectangle.
15+
*/
16+
public class Exercise15_18 extends javafx.application.Application {
17+
static final double WIDTH = 600;
18+
static final double HEIGHT = 600;
19+
static final Color FILL_COLOR = Color.TRANSPARENT;
20+
static final Color STROKE_COLOR = Color.BLACK;
21+
22+
public static void main(String[] args) {
23+
Application.launch(args);
24+
}
25+
26+
@Override
27+
public void start(Stage primaryStage) {
28+
Rectangle rec = rectangle();
29+
Pane pane = new Pane(rec);
30+
rec.setOnMouseDragged(e -> {
31+
rec.setX(e.getX() - rec.getWidth() / 2);
32+
rec.setY(e.getY() - rec.getHeight() / 2);
33+
});
34+
primaryStage.setScene(new Scene(pane, WIDTH, HEIGHT));
35+
primaryStage.setTitle("Move a rectangle using mouse");
36+
primaryStage.show();
37+
}
38+
39+
Rectangle rectangle() {
40+
Rectangle rec = new Rectangle(WIDTH / 2, HEIGHT / 2, 60, 30);
41+
rec.setFill(FILL_COLOR);
42+
rec.setStroke(STROKE_COLOR);
43+
return rec;
44+
}
45+
46+
}

‎ch_15/Exercise15_19.java‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ch_15;
2+
3+
import ch_09.exercise09_06.StopWatch;
4+
import javafx.application.Application;
5+
import javafx.scene.Scene;
6+
import javafx.scene.layout.Pane;
7+
import javafx.scene.paint.Color;
8+
import javafx.scene.shape.Circle;
9+
import javafx.scene.text.Text;
10+
import javafx.stage.Stage;
11+
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
14+
/**
15+
* **15.19 (Game: eye-hand coordination) Write a program that displays a circle of radius
16+
* 10 pixels filled with a random color at a random location on a pane, as shown
17+
* in Figure 15.29b. When you click the circle, it disappears and a new random color
18+
* circle is displayed at another random location. After twenty circles are
19+
* clicked, display the time spent in the pane, as shown in Figure 15.29c
20+
*/
21+
public class Exercise15_19 extends javafx.application.Application {
22+
private final double WIDTH = 600;
23+
private final double HEIGHT = 600;
24+
static final int MAX_CIRCLE_COUNT = 20;
25+
static final String TITLE_TEXT = "Exercise15_19";
26+
27+
public static void main(String[] args) {
28+
Application.launch(args);
29+
}
30+
31+
@Override
32+
public void start(Stage primaryStage) {
33+
AtomicInteger circleCount = new AtomicInteger();
34+
Circle circle = new Circle(0, 0, 10);
35+
drawCircle(circle);
36+
Pane pane = new Pane(circle);
37+
Text countText = new Text(50, 50, String.valueOf(circleCount));
38+
pane.getChildren().add(countText);
39+
StopWatch stopWatch = new StopWatch();
40+
circle.setOnMouseClicked(e -> {
41+
int currentCount = circleCount.get();
42+
if (currentCount == 0) {
43+
stopWatch.start();
44+
} else if (currentCount < MAX_CIRCLE_COUNT - 1) {
45+
circleCount.getAndIncrement();
46+
countText.setText(String.valueOf(circleCount));
47+
drawCircle(circle);
48+
} else {
49+
stopWatch.stop();
50+
circleCount.getAndIncrement();
51+
countText.setText(String.valueOf(circleCount));
52+
pane.getChildren().remove(circle);
53+
pane.getChildren().add(new Text(WIDTH / 2, HEIGHT / 2, "Time spent is " +
54+
stopWatch.getElapsedTime() + " milliseconds"));
55+
}
56+
});
57+
primaryStage.setScene(new Scene(pane, WIDTH, HEIGHT));
58+
primaryStage.setTitle(TITLE_TEXT);
59+
primaryStage.show();
60+
}
61+
62+
private void drawCircle(Circle circle) {
63+
double min = circle.getRadius() + 5;
64+
double max = WIDTH - circle.getRadius() - 5;
65+
circle.setCenterX((Math.random() * (max - min) + min));
66+
max = WIDTH - circle.getRadius() - 5;
67+
circle.setCenterY((Math.random() * (max - min) + min));
68+
circle.setFill(new Color(Math.random(), Math.random(), Math.random(), 1));
69+
}
70+
}

0 commit comments

Comments
(0)

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