-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBallPane.java
72 lines (60 loc) · 1.45 KB
/
BallPane.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class BallPane extends Pane
{
public final double radius = 20;
private double x = radius;
private double y = radius;
private double dx = 1;
private double dy = 1;
private Circle circle = new Circle(x,y,radius);
private Timeline animation;
public BallPane()
{
circle.setFill(Color.ORANGE);
getChildren().add(circle);
new KeyFrame(Duration.millis(50), e -> moveBall());
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}
public void play()
{
animation.play();
}
public void pause()
{
animation.pause();
}
public void increaseSpeed()
{
animation.setRate(animation.getRate() + 0.1);
}
public void decreaseSpeed()
{
animation.setRate(animation.getRate() > 0 ? animation.getRate() - 0.1 : 0);
}
public DoubleProperty rateProperty()
{
return animation.rateProperty();
}
protected void moveBall()
{
if(x < radius || x > getWidth() - radius)
{
dx =+ -1; // change the ball's direction of movement
}
if(y < radius || y > getHeight() - radius)
{
dy =+ -1; // change the ball's direction of movement
}
x =+ dx;
y =+ dy;
circle.setCenterX(x);
circle.setCenterY(y);
}
}