-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBolinha.java
74 lines (58 loc) · 1.59 KB
/
Bolinha.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
73
74
package edu.curso;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Bolinha extends Application {
class Ponto {
double x;
double y;
double velX = 5;
double velY = 0;
}
@Override
public void start(Stage primaryStage) throws Exception {
Group grp = new Group();
Scene scn = new Scene(grp);
primaryStage.setScene(scn);
Canvas canvas = new Canvas(640, 480);
GraphicsContext ctx = canvas.getGraphicsContext2D();
grp.getChildren().add(canvas);
final Ponto p = new Ponto();
new AnimationTimer() {
@Override
public void handle(long now) {
ctx.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
ctx.setStroke(Color.DARKBLUE);
ctx.setFill(Color.BLUE);
ctx.beginPath();
ctx.fillOval(p.x, p.y, 50, 50);
p.x += p.velX;
p.y += p.velY;
if ((p.x > canvas.getWidth() - 51) && (p.y == 0)) {
p.velX = 0;
p.velY = 5;
} else if ((p.y > canvas.getHeight() - 51) && (p.x > canvas.getWidth() - 51)) {
p.velY = 0;
p.velX = -5;
} else if ((p.x == 0) && (p.y == 0)) {
p.velX = 0;
p.velY = 0;
} else if ((p.x == 0) && (p.y > canvas.getHeight() - 51)) {
p.velX = 0;
p.velY = -5;
}
ctx.stroke();
}
}.start();
primaryStage.setTitle("Animação com bolinha");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(Bolinha.class, args);
}
}