-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4K.java
More file actions
87 lines (72 loc) · 2.38 KB
/
Copy pathExample4K.java
File metadata and controls
87 lines (72 loc) · 2.38 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
76
77
78
79
80
81
82
83
84
85
86
87
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.event.EventHandler;
import javafx.scene.input.KeyEvent;
import java.util.ArrayList;
// Keyboard events
public class Example4K extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage theStage)
{
theStage.setTitle( "Keyboard Example" );
Group root = new Group();
Scene theScene = new Scene( root );
theStage.setScene( theScene );
Canvas canvas = new Canvas( 512 - 64, 256 );
root.getChildren().add( canvas );
ArrayList<String> input = new ArrayList<String>();
theScene.setOnKeyPressed(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
// only add once... prevent duplicates
if ( !input.contains(code) )
input.add( code );
}
});
theScene.setOnKeyReleased(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String code = e.getCode().toString();
input.remove( code );
}
});
GraphicsContext gc = canvas.getGraphicsContext2D();
Image left = new Image( "left.png" );
Image leftG = new Image( "leftG.png" );
Image right = new Image( "right.png" );
Image rightG = new Image( "rightG.png" );
new AnimationTimer()
{
public void handle(long currentNanoTime)
{
// Clear the canvas
gc.clearRect(0, 0, 512,512);
if (input.contains("LEFT"))
gc.drawImage( leftG, 64, 64 );
else
gc.drawImage( left, 64, 64 );
if (input.contains("RIGHT"))
gc.drawImage( rightG, 256, 64 );
else
gc.drawImage( right, 256, 64 );
}
}.start();
theStage.show();
}
}