Java Reference
In-Depth Information
Each ball has its state: the
x
-,
y
-coordinates, color, and direction to move. You can
define a class named
Ball
that extends
javafx.scene.shape.Circle
. The
x
-,
y
-coordinates and the color are already defined in
Circle
. When a ball is created, it
starts from the upper-left corner and moves downward to the right. A random color is
assigned to a new ball.
The
MultiplBallPane
class is responsible for displaying the ball and the
MultipleBounceBall
class places the control components and implements the control. The
relationship of these classes is shown in FigureĀ 20.9. Listing 20.6 gives the program.
javafx.scene.shape.Circle
javafx.scene.layout.Pane
javafx.application.Application
m1
11
Ball
MultipleBallPane
MultipleBounceBall
dx: double
dy: double
-animation: Timeline
+MultipleBallPane()
+play(): void
+pause(): void
+increaseSpeed(): void
+decreaseSpeed(): void
+rateProperty(): Double
Property
+moveBall(): void
+Ball (x: double, y: double
radius: double,
color: color)
F
IGURE
20.9
MultipleBounceBall
contains
MultipleBallPane
, and
MultipleBallPane
contains
Ball
.
L
ISTING
20.6
MultipleBounceBall.java
1
import
javafx.animation.KeyFrame;
2
import
javafx.animation.Timeline;
3
import
javafx.application.Application;
4
import
javafx.beans.property.DoubleProperty;
5
import
javafx.geometry.Pos;
6
import
javafx.scene.Node;
7
import
javafx.stage.Stage;
8
import
javafx.scene.Scene;
9
import
javafx.scene.control.Button;
10
import
javafx.scene.control.ScrollBar;
11
import
javafx.scene.layout.BorderPane;
12
import
javafx.scene.layout.HBox;
13
import
javafx.scene.layout.Pane;
14
import
javafx.scene.paint.Color;
15
import
javafx.scene.shape.Circle;
16
import
javafx.util.Duration;
17
18
public class
MultipleBounceBall
extends
Application {
19 @Override
// Override the start method in the Application class
20
public void
start(Stage primaryStage) {
21 MultipleBallPane ballPane =
new
MultipleBallPane();
22 ballPane.setStyle(
"-fx-border-color: yellow"
);
23
24 Button btAdd =
new
Button(
"+"
);
create a ball pane
set ball pane border
create buttons
Search WWH ::
Custom Search