Bonjour � tous,
J'essaie actuellement d'utiliser un canvas pour dessiner un certains nombre d'objects graphiques represent�s par une ImageView.
Les objects sont bien affich�s mais le probl�me est que les eventHandler de mes imageView ne fonctionnent pas.
Auriez-vous des conseils sur la bonne fa�on de proc�der.
Voici mon code de test sur les canvas :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
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 import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { static Scene scene; /** * This is the program's entry point and launches the application. * * @param args An array of arguments (usually specified on the command line). */ public static void main(final String[] args) { Application.launch(args); } /* * @see javafx.application.Application#start(javafx.stage.Stage) */ @Override public void start(final Stage stage) { stage.setTitle("JavaFX layer Test"); Group root = new Group(); Layer layer = new Layer(800,600); root.getChildren().add(layer.getCanvas()); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } }
Code : S�lectionner tout - Visualiser dans une fen�tre � part
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
88
89
90
91
92
93
94
95 import javafx.animation.AnimationTimer; import javafx.geometry.Point3D; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; import javafx.scene.transform.Rotate; public class Layer { private double width; private double height; private Canvas canvas; private final int NB=12; private MovingObject[] movingObjects; /** * Create a new Layer. * * @param type type of the layer * @param width width of the layer * @param height height of the layer */ public Layer(double width,double height) { this.width = width; this.height = height; canvas = new Canvas(width,height); populateLayer(); new AnimationTimer() { @Override public void handle(final long now) {renderFrame();} }.start(); } public Canvas getCanvas() { return canvas; } private void populateLayer(){ movingObjects = new MovingObject[NB]; double x = 20; double y= 20; for(int i = 0; i<NB ;i++){ movingObjects[i] = new MovingObject("Object["+i+"]", 90 , new Point3D(x, y, 2500),16,16); x = x+20; if(x>=780){ x =20; y = y +20; } } } public void renderFrame() { canvas.getGraphicsContext2D().clearRect(0, 0, width, height); for (final MovingObject currentObject : movingObjects) { drawRotatedImage(canvas.getGraphicsContext2D(), currentObject.getImg().getImage(), currentObject.getHeading(), currentObject.getPosition().getX(), currentObject.getPosition().getY()); currentObject.setHeading(currentObject.getHeading()+1); } } /** * Draws an image on a graphics context. * * The image is drawn at (tlpx, tlpy) rotated by angle pivoted around the point: * (tlpx + image.getWidth() / 2, tlpy + image.getHeight() / 2) * * @param gc the graphics context the image is to be drawn on. * @param angle the angle of rotation. * @param tlpx the top left x co-ordinate where the image will be plotted (in canvas co-ordinates). * @param tlpy the top left y co-ordinate where the image will be plotted (in canvas co-ordinates). */ private void drawRotatedImage(GraphicsContext gc, Image image, double angle, double tlpx, double tlpy) { gc.save(); // saves the current state on stack, including the current transform rotate(gc, angle, tlpx + image.getWidth() / 2, tlpy + image.getHeight() / 2); gc.drawImage(image, tlpx, tlpy); gc.restore(); // back to original state (before rotation) } /** * Sets the transform for the GraphicsContext to rotate around a pivot point. * * @param gc the graphics context the transform to applied to. * @param angle the angle of rotation. * @param px the x pivot co-ordinate for the rotation (in canvas co-ordinates). * @param py the y pivot co-ordinate for the rotation (in canvas co-ordinates). */ private void rotate(GraphicsContext gc, double angle, double px, double py) { Rotate r = new Rotate(angle, px, py); gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy()); } }
Code : S�lectionner tout - Visualiser dans une fen�tre � part
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 import javafx.event.EventHandler; import javafx.geometry.Point3D; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; /** * Base class for Moving Object */ public class MovingObject { // -------------------------------------------- // ATTRIBUTES // -------------------------------------------- /** * Object id used as key */ private String objectId; /** * Current heading for moving Object */ private double heading; /** * Current position */ private Point3D position; private ImageView img; // ------------------------------------------------------------------------- // CONSTRUCTOR // ------------------------------------------------------------------------- /** * Create a new moving object. * * @param name * name of the object * @param heading * heading of the object * @param x * x position of the object * @param y * y position of the object */ public MovingObject(String objectId, double heading, Point3D position,double width, double height) { img = new ImageView(new Image("MovingObject.png")); this.objectId = objectId; this.heading = heading; this.position = position; img.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { System.out.println("Clique on object:" + objectId); } }); } // ------------------------------------------------------------------------- // METHODS // ------------------------------------------------------------------------- /** * Change the heading of the object * * @param heading * new heading value (from 0.0 for North to 360.0) */ public void setHeading(double heading) { this.heading = heading; } /** * Change the position of the object * * @param Point3D * new position point */ public void setPosition(Point3D position) { this.position = position; } /** * Retrieve the object id * * @return the flight id */ public String getObjectId() { return objectId; } /** * Retrieve the heading of this flight . * * @return the heading */ public double getHeading() { return heading; } /** * Retrieve the position of this flight . * * @return the position coordinate */ public Point3D getPosition() { return position; } public ImageView getImg() { return img; } }
Partager