-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathExample3T.java
More file actions
75 lines (59 loc) · 2.31 KB
/
Copy pathExample3T.java
File metadata and controls
75 lines (59 loc) · 2.31 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
// An alternative implementation of Example 3,
// using the Timeline, KeyFrame, and Duration classes.
// Animation of Earth rotating around the sun. (Hello, world!)
public class Example3T extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage theStage)
{
theStage.setTitle( "Timeline Example" );
Group root = new Group();
Scene theScene = new Scene( root );
theStage.setScene( theScene );
Canvas canvas = new Canvas( 512, 512 );
root.getChildren().add( canvas );
GraphicsContext gc = canvas.getGraphicsContext2D();
Image earth = new Image( "earth.png" );
Image sun = new Image( "sun.png" );
Image space = new Image( "space.png" );
Timeline gameLoop = new Timeline();
gameLoop.setCycleCount( Timeline.INDEFINITE );
final long timeStart = System.currentTimeMillis();
KeyFrame kf = new KeyFrame(
Duration.seconds(0.017), // 60 FPS
new EventHandler<ActionEvent>()
{
public void handle(ActionEvent ae)
{
double t = (System.currentTimeMillis() - timeStart) / 1000.0;
double x = 232 + 128 * Math.cos(t);
double y = 232 + 128 * Math.sin(t);
// Clear the canvas
gc.clearRect(0, 0, 512,512);
// background image clears canvas
gc.drawImage( space, 0, 0 );
gc.drawImage( earth, x, y );
gc.drawImage( sun, 196, 196 );
}
});
gameLoop.getKeyFrames().add( kf );
gameLoop.play();
theStage.show();
}
}