Java Reference
In-Depth Information
84 animation.pause();
85 }
86
87 public void increaseSpeed() {
88 animation.setRate(animation.getRate() + 0.1 );
89 }
90
91 public void decreaseSpeed() {
92 animation.setRate(
93 animation.getRate() > 0 ? animation.getRate() - 0.1 : 0 );
94 }
95
96
public DoubleProperty rateProperty() {
97
return animation.rateProperty();
98 }
99
100 protected void moveBall() {
101 for (Node node: this .getChildren()) {
102 Ball ball = (Ball)node;
103 // Check boundaries
104 if (ball.getCenterX() < ball.getRadius() ||
105 ball.getCenterX() > getWidth() - ball.getRadius()) {
106 ball.dx *= -1 ; // Change ball move direction
107 }
108 if (ball.getCenterY() < ball.getRadius() ||
109 ball.getCenterY() > getHeight() - ball.getRadius()) {
110 ball.dy *= -1 ; // Change ball move direction
111 }
112
113
move all balls
change x direction
change y direction
// Adjust ball position
114
ball.setCenterX(ball.dx + ball.getCenterX());
adjust ball positions
115
ball.setCenterY(ball.dy + ball.getCenterY());
116 }
117 }
118 }
119
120
class Ball extends Circle {
121
private double dx = 1 , dy = 1 ;
declare d x and d y
122
123 Ball( double x, double y, double radius, Color color) {
124 super (x, y, radius);
125 setFill(color); // Set ball color
126 }
127 }
128 }
create a ball
The add() method creates a new ball with a random color and adds it to the pane (line
70). The pane stores all the balls in a list. The subtract() method removes the last ball in
the list (line 75).
When the user clicks the
+
button, a new ball is added to the pane (line 31). When the user
clicks the
button, the last ball in the array list is removed (line 32).
The moveBall() method in the MultipleBallPane class gets every ball in the pane's
list and adjusts the balls' positions (lines 114-115).
-
20.23
What is the return value from invoking pane.getChildre() for a pane?
Check
Point
 
Search WWH ::




Custom Search