IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaFX Discussion :

Canvas et event Handler


Sujet :

JavaFX

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2015
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juin 2015
    Messages : 4
    Points : 8
    Points
    8
    Par défaut Canvas et event Handler
    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;
        }
     
    }

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Ben c'est simple : tu fais du dessin bitmap en utilisant des ImageView, ok; pourquoi pas... Mais du coup ça veut dire également que ces mêmes objets ne sont pas dans la scène et donc qu'ils ne risquent pas de recevoir des événements clavier, souris, tactile, etc. Ça se voit assez clairement si tu attaches ScenicView à ton programme : tu n'as que 2 nœuds, la racine de la scène et le Canvas. Ici, la seule chose qui peut recevoir des événement c'est ton Canvas.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

Discussions similaires

  1. Réponses: 6
    Dernier message: 27/08/2007, 13h37
  2. event handler et accès anonyme
    Par @melie dans le forum SharePoint
    Réponses: 5
    Dernier message: 25/08/2007, 19h17
  3. Réponses: 1
    Dernier message: 15/08/2007, 15h45
  4. Birt Event Handler
    Par medbass dans le forum BIRT
    Réponses: 8
    Dernier message: 07/08/2006, 14h51
  5. Problème avec event handler
    Par MASSAKA dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 15/11/2005, 09h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo