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