-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathExample3AI.java
More file actions
68 lines (55 loc) · 1.94 KB
/
Copy pathExample3AI.java
File metadata and controls
68 lines (55 loc) · 1.94 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
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.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
// Animation of Earth rotating around the sun. (Hello, world!)
public class Example3AI extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage theStage)
{
theStage.setTitle( "AnimatedImage 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" );
AnimatedImage ufo = new AnimatedImage();
Image[] imageArray = new Image[6];
for (int i = 0; i < 6; i++)
imageArray[i] = new Image( "ufo_" + i + ".png" );
ufo.frames = imageArray;
ufo.duration = 0.100;
final long startNanoTime = System.nanoTime();
new AnimationTimer()
{
public void handle(long currentNanoTime)
{
double t = (currentNanoTime - startNanoTime) / 1000000000.0;
double x = 232 + 128 * Math.cos(t);
double y = 232 + 128 * Math.sin(t);
gc.drawImage( space, 0, 0 );
gc.drawImage( earth, x, y );
gc.drawImage( sun, 196, 196 );
gc.drawImage( ufo.getFrame(t), 450, 25 );
}
}.start();
theStage.show();
}
}