Skip to content

Commit 4ac1f2d

Browse files
committed
commit Exercise_30_21
1 parent 8fdad69 commit 4ac1f2d

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package exercise_30_21;
2+
3+
import javafx.application.Application;
4+
import javafx.geometry.Pos;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.Button;
7+
import javafx.scene.layout.BorderPane;
8+
import javafx.scene.layout.HBox;
9+
import javafx.stage.Stage;
10+
11+
public class Exercise_30_21 extends Application {
12+
13+
@Override
14+
public void start(Stage primaryStage) {
15+
MultipleBallPane ballPane = new MultipleBallPane();
16+
ballPane.setStyle("-fx-border-color: yellow");
17+
18+
Button btSuspend = new Button("Suspend");
19+
Button btResume = new Button("Resume");
20+
Button btAdd = new Button("+");
21+
Button btSubtract = new Button("-");
22+
HBox hBox = new HBox(10);
23+
hBox.getChildren().addAll(btSuspend, btResume, btAdd, btSubtract);
24+
hBox.setAlignment(Pos.CENTER);
25+
26+
btSuspend.setOnAction(e -> ballPane.suspend());
27+
btResume.setOnAction(e -> ballPane.resume());
28+
btAdd.setOnAction(e -> ballPane.add());
29+
btSubtract.setOnAction(e -> ballPane.subtract());
30+
31+
BorderPane pane = new BorderPane();
32+
pane.setCenter(ballPane);
33+
pane.setBottom(hBox);
34+
35+
Scene scene = new Scene(pane, 600, 400);
36+
primaryStage.setTitle("Exercise_30_21");
37+
primaryStage.setScene(scene);
38+
primaryStage.show();
39+
}
40+
41+
public static void main(String[] args) {
42+
launch(args);
43+
}
44+
45+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package exercise_30_21;
2+
3+
import exercise_20_05.Ball;
4+
import javafx.application.Platform;
5+
import javafx.scene.layout.Pane;
6+
import javafx.scene.paint.Color;
7+
8+
public class MultipleBallPane extends Pane {
9+
private int sleepTime;
10+
private Thread thread = new Thread(() -> {
11+
try {
12+
while(true) {
13+
Platform.runLater(() -> moveBall());
14+
Thread.sleep(sleepTime);
15+
}
16+
}
17+
catch (InterruptedException ex) {
18+
ex.printStackTrace();
19+
}
20+
});
21+
22+
public MultipleBallPane() {
23+
sleepTime = 10;
24+
thread.start();
25+
}
26+
27+
public void setSleepTime(int sleepTime) {
28+
this.sleepTime = sleepTime;
29+
}
30+
31+
public void add() {
32+
Color color = new Color(Math.random(), Math.random(), Math.random(), 0.5);
33+
Ball ball = new Ball(30, 30, 20, color);
34+
getChildren().add(ball);
35+
ball.setOnMousePressed(e -> getChildren().remove(ball));
36+
}
37+
38+
public void add(Ball ball) {
39+
ball.setOnMousePressed(e -> getChildren().remove(ball));
40+
getChildren().add(ball);
41+
}
42+
43+
public void subtract() {
44+
if(getChildren().size() > 0) {
45+
getChildren().remove(getChildren().size() - 1);
46+
}
47+
}
48+
49+
public void resume() {
50+
thread.resume();
51+
}
52+
53+
public void suspend() {
54+
thread.suspend();
55+
}
56+
57+
public void increaseSpeed() {
58+
if(sleepTime > 1) {
59+
sleepTime--;
60+
}
61+
}
62+
63+
public void decreaseSpeed() {
64+
sleepTime++;
65+
}
66+
67+
protected void moveBall() {
68+
for(int i = 0; i < getChildren().size(); i++) {
69+
Ball ball = (Ball)getChildren().get(i);
70+
if(ball.getCenterX() > getWidth() - ball.getRadius() ||
71+
ball.getCenterX() < ball.getRadius()) {
72+
ball.setDx(ball.getDx() * (- 1));
73+
}
74+
if(ball.getCenterY() > getHeight() - ball.getRadius() ||
75+
ball.getCenterY() < ball.getRadius()) {
76+
ball.setDy(ball.getDy() * (- 1));
77+
}
78+
79+
ball.setCenterX(ball.getCenterX() + ball.getDx());
80+
ball.setCenterY(ball.getCenterY() + ball.getDy());
81+
82+
for(int j = i + 1; j < getChildren().size(); j++) {
83+
Ball ball2 = (Ball)getChildren().get(j);
84+
if(isIntersect(ball, ball2)) {
85+
double radius = ball.getRadius() + ball2.getRadius();
86+
double centerX;
87+
double centerY;
88+
89+
if(ball.getCenterX() - radius < 0) {
90+
centerX = radius;
91+
}
92+
else if(ball.getCenterX() > getWidth() - radius){
93+
centerX = getWidth() - radius;
94+
}
95+
else {
96+
centerX = ball.getCenterX();
97+
}
98+
99+
if(ball.getCenterY() - radius < 0) {
100+
centerY = radius;
101+
}
102+
else if(ball.getCenterY() > getHeight()- radius){
103+
centerY = getHeight()- radius;
104+
}
105+
else {
106+
centerY = ball.getCenterY();
107+
}
108+
109+
Ball newBall = new Ball(centerX, centerY, radius, ball.getColor());
110+
getChildren().removeAll(ball, ball2);
111+
add(newBall);
112+
}
113+
}
114+
}
115+
}
116+
117+
protected boolean isIntersect(Ball b1, Ball b2) {
118+
if(Math.abs(b1.getCenterX() - b2.getCenterX()) <= b1.getRadius() + b2.getRadius() &&
119+
Math.abs(b1.getCenterY() - b2.getCenterY()) <= b1.getRadius() + b2.getRadius()) {
120+
return true;
121+
}
122+
else {
123+
return false;
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)