-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyingBox.java
More file actions
55 lines (49 loc) · 1.59 KB
/
FlyingBox.java
File metadata and controls
55 lines (49 loc) · 1.59 KB
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
//
// FlyingBox: each object represents an animated "flying" rectangle in
// the "game"
//
//import javafx.event.Event;
import javafx.scene.shape.Rectangle;
import javafx.application.Platform;
import javafx.scene.paint.Color;
//import javafx.application.Platform;
import javafx.scene.layout.Pane;
public class FlyingBox extends Rectangle
{
private int xDirection = 1;
private int yDirection = 1;
private int dirRate = 2;
private Pane area;
public FlyingBox(Pane gamePane, int x, int y, int w, int h, int c)
{
// super(w, h, Color.color(0.4, 0.2, 0.6, 1.0));
// make RGB values 0.0-1.0 for Color from a single int using each decimal digit as most significant
super(w, h, Color.color(((c / 100.0) - (c / 100)), ((c / 10.0) - (c / 10)), ((c / 1000.0) - (c / 1000)), 1.0));
// System.out.println("made a rectangle");
// System.out.println("RGB: " + ((c / 100.0) - (c / 100)) + " " + ((c / 10.0) -
// (double) (c / 10)) + " " + ((c / 1000.0) - (double) (c / 1000)));
area = gamePane;
Platform.runLater(() -> {
setX(x);
setY(y);
setWidth(w);
setHeight(h);
});
}
// update the box's position; x,y is upper left corner on a box
// so we need to add its size when checking the right and bottom
// bounds
public void move()
{
if (getX() + getWidth() > area.getWidth() || getX() < 0) {
xDirection = -xDirection;
}
if (getY() + getHeight() > area.getHeight() || getY() < 0) {
yDirection = -yDirection;
}
Platform.runLater(() -> {
setX(getX() + dirRate * xDirection);
setY(getY() + dirRate * yDirection);
});
}
}