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 :

En JavaFX, comment manipuler les images du jeu slot machine


Sujet :

JavaFX

  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 1
    Par défaut En JavaFX, comment manipuler les images du jeu slot machine
    Bonjour,

    En JavaFX, comment manipuler les images du jeu "slot machine", actionner et arrêter l'animation ?

    J'ai commencé mais je trouve des difficultés :

    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
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    public class M1 extends Application {
     
     
        DropShadow shadow = new DropShadow();
        Label label = new Label();
     
        public static void main(String[] args) {
     
            launch(args);
        }
     
     
        @Override
        public void start(Stage stage) {
            stage.setTitle("SLOT8");
            Group root = new Group();
            Scene scene = new Scene(root, 600, 600, Color.RED);
     
     
            stage.setScene(scene);
            stage.setResizable(true);
     
                   stage.setWidth(600);
            stage.setHeight(600);
            stage.setScene(scene); 
            stage.sizeToScene(); 
     
                             //µµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµ
                Rectangle fond_ = new Rectangle();
                fond_.setWidth(500);
                fond_.setHeight(8);
                fond_.setArcWidth(30);
                fond_.setArcHeight(30);
                fond_.setFill(Color.BLACK);
                fond_.setTranslateX(60);//on positionne le groupe plutôt que le rectangle
                fond_.setTranslateY(175);
     
     
     
     
     
     
                ImageView image1 = new ImageView("bell.png");
                ImageView image2 = new ImageView("bar.png");
                ImageView image3 = new ImageView("clover.png");
                ImageView image4 = new ImageView("banana.png");
                ImageView image5 = new ImageView("banana.png");
                ImageView image6 = new ImageView("bell.png");
                ImageView image7= new ImageView("bar.png");
                ImageView image8= new ImageView("clover.png");
                ImageView image9 = new ImageView("banana.png");
                ImageView image16 = new ImageView("bell.png");
                ImageView image15 = new ImageView("bar.png");
                ImageView image17 = new ImageView("clover.png");
                ImageView image14 = new ImageView("banana.png");
                ImageView image13 = new ImageView("banana.png");
                ImageView image18 = new ImageView("bell.png");
                ImageView image12= new ImageView("bar.png");
                ImageView image19= new ImageView("clover.png");
                ImageView image11 = new ImageView("banana.png");
                ImageView image21 = new ImageView("bell.png");
                ImageView image22 = new ImageView("bar.png");
                ImageView image23 = new ImageView("clover.png");
                ImageView image24 = new ImageView("banana.png");
                ImageView image25 = new ImageView("banana.png");
                ImageView image26 = new ImageView("bell.png");
                ImageView image27= new ImageView("bar.png");
                ImageView image28= new ImageView("clover.png");
                ImageView image29 = new ImageView("banana.png");
     
     
     
          VBox vbox1 = new VBox();
          vbox1.setLayoutX(60);
          vbox1.setLayoutY(10);
     
          VBox vbox2 = new VBox();
          vbox2.setLayoutX(240);
          vbox2.setLayoutY(10);
     
     
     
          VBox vbox3 = new VBox();
          vbox3.setLayoutX(650);
          vbox3.setLayoutY(10);
     
     
     
     
     
            Button button33 = new Button("33");
            button33.setLayoutX(400);
            button33.setLayoutY(400);
     
            button33.addEventHandler(MouseEvent.MOUSE_ENTERED,
                    new EventHandler<MouseEvent>() {
               public void handle(MouseEvent e) {
                    button33.setEffect(shadow);
     
                }
            });
     
            button33.addEventHandler(MouseEvent.MOUSE_EXITED,
                    new EventHandler<MouseEvent>() {
                public void handle(MouseEvent e) {
                    button33.setEffect(shadow);
                }
            });
     
     
     
     
     
          ImageView[] images = { image1, image2, image3, image4, image5, image6, image7, image8, image9};
          ImageView[] images2 = { image11, image12, image13, image14, image15, image16, image17, image18, image19};
          ImageView[] images3 = { image21, image22, image23, image24, image25, image26, image27, image28, image29};
     
     
     
           for (ImageView i : images) {
           vbox1.getChildren().add(i);
     
               }
           for (ImageView i2 : images2) {
               vbox2.getChildren().add(i2);}
           for (ImageView i3 : images3) {
               vbox2.getChildren().add(i3);}
           for (ImageView i3 : images3) {
           for (ImageView i2  : images2) {
          for (ImageView i  : images) {
      TranslateTransition translateTransition2 =                    		  
            		  new TranslateTransition(Duration.millis(2000), i2);
     
              TranslateTransition translateTransition =           
            		          		  new TranslateTransition(Duration.millis(4000), i);
              TranslateTransition translateTransition3 =           
              		  new TranslateTransition(Duration.millis(4000), i3);
     
              translateTransition.setFromX(i.getX());
              translateTransition.setFromY(i.getY()-800 );
             translateTransition.setToY((i.getY()  +20 ));
              translateTransition.setCycleCount(2);
              translateTransition.setAutoReverse(true);
     
              translateTransition2.setFromX(i2.getX());
              translateTransition2.setFromY(i2.getY()-800 );
             translateTransition2.setToY((i2.getY()  +20 ));
              translateTransition2.setCycleCount(2);
              translateTransition2	.setAutoReverse(true);
     
              translateTransition3.setFromX(i3.getX());
              translateTransition3.setFromY(i3.getY()-800 );
             translateTransition3.setToY((i3.getY()  +20 ));
              translateTransition3.setCycleCount(2);
              translateTransition3	.setAutoReverse(true);
     
     
              ParallelTransition    parallelTransition = new ParallelTransition();
              parallelTransition.getChildren().addAll(
     
                      translateTransition,
                      translateTransition2,
                      translateTransition3
              );
              parallelTransition.setCycleCount(Timeline.INDEFINITE);
              parallelTransition.play();}}}
            root.getChildren().add(fond_);
     
            root.getChildren().add(sun);
     
     
     
            //  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
     
            Rectangle clavier = new Rectangle();
            clavier.setWidth(500);
            clavier.setHeight(200);
            clavier.setArcWidth(30);
            clavier.setArcHeight(30);
            clavier.setFill( 
                new LinearGradient(0f, 0f, 0f, 1f, true, CycleMethod.NO_CYCLE,
                    new Stop[] {
                        new Stop(0, Color.web("#333333")),
                        new Stop(1, Color.web("#000000"))
                    }
                )
            );
            Reflection r = new Reflection();
            r.setFraction(0.25);
            r.setBottomOpacity(0);
            r.setTopOpacity(0.5);
            clavier.setEffect(r);
     
            clavier.setTranslateX(50);
            clavier.setTranslateY(350);
     
     
     
            root.getChildren().add(vbox1);
     
            root.getChildren().add(vbox2);
           root.getChildren().add(vbox3);
           root.getChildren().add(clavier );
     
     
     
           stage.setScene(scene);
            stage.show();                       
     
        }  
    }

  2. #2
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 897
    Billets dans le blog
    54
    Par défaut
    Je me suis permis de simplifier ton code en animant non pas chaque image une a une mais son conteneur la VBox, ce qui permet déjà de simplifier drastiquement le nombre d'animation et aussi de d’éliminer quelques bugs graphiques. Le défaut ici est que désormais tous les cylindres ont la même animation, donc c'est au niveau de la boucle foreach que des tirages aléatoires doivent avoir lieu pour permettre de faire que chaque cylindre ait une vitesse d'animation ou une durée de répétition de son animation ou même un décalage vertical différents de ceux de son voisin.

    J'ai alors introduit une seconde animation destinée a arrêter les cylindres. Lorsqu'on veut stopper les cylindres, on remplace tout simplement l'animation normale par une autre qui les arrête. La même remarque sur l’aléatoire s'applique pour la procédure d’arrêt : elle aurait bien besoin d'une petite variation aléatoire pour que chaque cylindre ait sa propre vitesse et durée d’arrêt. Et il faut bien sur s'assurer que la coord finale de l’arrêt soit valide pour l'affichage / le calcul du résultat.

    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
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
     
    public class M1 extends Application {
     
        DropShadow shadow = new DropShadow();
        Label label = new Label();
     
        public static void main(String[] args) {
     
            launch(args);
        }
     
        @Override
        public void start(Stage stage) {
            stage.setTitle("SLOT8");
            Group root = new Group();
            Scene scene = new Scene(root, 600, 600, Color.RED);
     
            stage.setScene(scene);
            stage.setResizable(true);
     
            stage.setWidth(600);
            stage.setHeight(600);
            stage.setScene(scene);
            stage.sizeToScene();
     
            //µµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµµ
            Rectangle fond_ = new Rectangle();
            fond_.setWidth(500);
            fond_.setHeight(8);
            fond_.setArcWidth(30);
            fond_.setArcHeight(30);
            fond_.setFill(Color.BLACK);
            fond_.setTranslateX(60);//on positionne le groupe plutôt que le rectangle
            fond_.setTranslateY(175);
     
            ImageView image1 = new ImageView(getClass().getResource("bell.png").toExternalForm());
            ImageView image2 = new ImageView(getClass().getResource("bar.png").toExternalForm());
            ImageView image3 = new ImageView(getClass().getResource("clover.png").toExternalForm());
            ImageView image4 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image5 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image6 = new ImageView(getClass().getResource("bell.png").toExternalForm());
            ImageView image7 = new ImageView(getClass().getResource("bar.png").toExternalForm());
            ImageView image8 = new ImageView(getClass().getResource("clover.png").toExternalForm());
            ImageView image9 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image16 = new ImageView(getClass().getResource("bell.png").toExternalForm());
            ImageView image15 = new ImageView(getClass().getResource("bar.png").toExternalForm());
            ImageView image17 = new ImageView(getClass().getResource("clover.png").toExternalForm());
            ImageView image14 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image13 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image18 = new ImageView(getClass().getResource("bell.png").toExternalForm());
            ImageView image12 = new ImageView(getClass().getResource("bar.png").toExternalForm());
            ImageView image19 = new ImageView(getClass().getResource("clover.png").toExternalForm());
            ImageView image11 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image21 = new ImageView(getClass().getResource("bell.png").toExternalForm());
            ImageView image22 = new ImageView(getClass().getResource("bar.png").toExternalForm());
            ImageView image23 = new ImageView(getClass().getResource("clover.png").toExternalForm());
            ImageView image24 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image25 = new ImageView(getClass().getResource("banana.png").toExternalForm());
            ImageView image26 = new ImageView(getClass().getResource("bell.png").toExternalForm());
            ImageView image27 = new ImageView(getClass().getResource("bar.png").toExternalForm());
            ImageView image28 = new ImageView(getClass().getResource("clover.png").toExternalForm());
            ImageView image29 = new ImageView(getClass().getResource("banana.png").toExternalForm());
     
            VBox vbox1 = new VBox();
            vbox1.setLayoutX(60);
            vbox1.setLayoutY(10);
     
            VBox vbox2 = new VBox();
            vbox2.setLayoutX(240);
            vbox2.setLayoutY(10);
     
            VBox vbox3 = new VBox();
            vbox3.setLayoutX(650);
            vbox3.setLayoutY(10);
     
            Button button33 = new Button("33");
            button33.setLayoutX(400);
            button33.setLayoutY(400);
     
            button33.addEventHandler(MouseEvent.MOUSE_ENTERED,
                    new EventHandler<MouseEvent>() {
                public void handle(MouseEvent e) {
                    button33.setEffect(shadow);
     
                }
            });
     
            button33.addEventHandler(MouseEvent.MOUSE_EXITED,
                    new EventHandler<MouseEvent>() {
                public void handle(MouseEvent e) {
                    button33.setEffect(shadow);
                }
            });
     
            ImageView[] images = {image1, image2, image3, image4, image5, image6, image7, image8, image9};
            ImageView[] images2 = {image11, image12, image13, image14, image15, image16, image17, image18, image19};
            ImageView[] images3 = {image21, image22, image23, image24, image25, image26, image27, image28, image29};
     
            for (ImageView i : images) {
                vbox1.getChildren().add(i);
            }
            for (ImageView i2 : images2) {
                vbox2.getChildren().add(i2);
            }
            for (ImageView i3 : images3) {
                vbox2.getChildren().add(i3);
            }
    //        for (ImageView i3 : images3) {
    //            for (ImageView i2 : images2) {
    //                for (ImageView i : images) {
    //                }
    //            }
    //        }
            root.getChildren().add(fond_);
     
    //        root.getChildren().add(sun);
            //  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
            Rectangle clavier = new Rectangle();
            clavier.setWidth(500);
            clavier.setHeight(200);
            clavier.setArcWidth(30);
            clavier.setArcHeight(30);
            clavier.setFill(
                    new LinearGradient(0f, 0f, 0f, 1f, true, CycleMethod.NO_CYCLE,
                            new Stop[]{
                                new Stop(0, Color.web("#333333")),
                                new Stop(1, Color.web("#000000"))
                            }
                    )
            );
            Reflection r = new Reflection();
            r.setFraction(0.25);
            r.setBottomOpacity(0);
            r.setTopOpacity(0.5);
            clavier.setEffect(r);
     
            clavier.setTranslateX(50);
            clavier.setTranslateY(350);
     
            root.getChildren().add(vbox1);
     
            root.getChildren().add(vbox2);
            root.getChildren().add(vbox3);
            root.getChildren().add(clavier);
     
            stage.setScene(scene);
            stage.show();
            ToggleButton spinButton = new ToggleButton("Start!");
            spinButton.setOnAction(actionEvent -> {
                if (spinButton.isSelected()) {
                    spinCylinders(vbox1, vbox2, vbox3);
                } else {
                    stopCylinders(vbox1, vbox2, vbox3);
                }
            });
            root.getChildren().add(spinButton);
        }
     
        private Animation currentAnimation = null;
     
        private void spinCylinders(Node... cylinders) {
            if (currentAnimation != null) {
                currentAnimation.pause();
                currentAnimation = null;
            }
            // Actuellement avec cette simplification du code chaque cy;indre subit la meme animation.
            // Introduire une part d'aleatoire sur chaque cylinder pour qu'ils puissent tourner indepandement les uns des autres ?
            // - Duree d'animation ;
            // - Offset vertical ;
            // - Nombre de cycles.
            ParallelTransition parallelTransition = new ParallelTransition();
            for (Node cylinder : cylinders) {
                TranslateTransition translateTransition
                        = new TranslateTransition(Duration.millis(2000), cylinder);
                translateTransition.setFromX(cylinder.getTranslateX());
                translateTransition.setFromY(cylinder.getTranslateY() - 800);
                translateTransition.setToY(cylinder.getTranslateY() + 20);
                translateTransition.setCycleCount(2);
                translateTransition.setAutoReverse(true);
                parallelTransition.getChildren().add(translateTransition);
            }
            parallelTransition.setCycleCount(Timeline.INDEFINITE);
            parallelTransition.play();
            currentAnimation = parallelTransition;
        }
     
        private void stopCylinders(Node... cylinders) {
            if (currentAnimation == null) {
                return;
            }
            currentAnimation.pause();
            currentAnimation = null;
            // Idem, introduire de l'aleatoire pour chaque cylindre:
            // - Duree d'extinction ;
            // - Offset vertical.
            ParallelTransition parallelTransition = new ParallelTransition();
            for (Node cylinder : cylinders) {
                TranslateTransition translateTransition
                        = new TranslateTransition(Duration.millis(2000), cylinder);
                translateTransition.setFromX(cylinder.getTranslateX());
                translateTransition.setFromY(cylinder.getTranslateY());
                // Precalculer un Y final qui soit correct !!!!!!!!!!!!!!!!
                translateTransition.setToY(cylinder.getTranslateY() + 300);
                // Ou un interpolateur fait maison !
                translateTransition.setInterpolator(Interpolator.EASE_OUT);
                parallelTransition.getChildren().add(translateTransition);
            }
            parallelTransition.play();
            currentAnimation = parallelTransition;
        }
    }
    PS : je n'aurais pas codé tout a fait ça comme ça. Au lieu d'avoir une seule VBox qu'on fait bouger vers le haut ou vers le bas, j'aurai utilisé 2~3 ImageView (aussi intégrées a une VBox ou un Group) qu'on décale toujours dans le même sens et qu'on repositionne dans le contrôle parent une fois hors-champs. Lorsque la première contenue dans le parent sort de la zone d'affichage, on la sort du parent et on la repositionne en fin de liste, on change alors l'image affichée dedans. En fait ici on a que 4 images différentes donc ça ne sert a rien d'avoir autant de chargements d'image ni autant d'ImageView.

    Également j'aurais tendance a coder chaque cylindre dans un contrôle séparé (donc une classe / contrôle Cylindre qu'on réutilise 3 fois). Mais bon, je ne suis pas spécialiste en jeu donc j'ignore si c'est la meilleure démarche. Dans ce domaine la recherche de perfs est souvent plus importante que la bonne conception.
    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: 3
    Dernier message: 17/01/2013, 19h46
  2. [Débutant] Comment manipuler les images sur WP ?
    Par insane_80 dans le forum Windows Phone
    Réponses: 2
    Dernier message: 08/11/2012, 12h54
  3. Réponses: 3
    Dernier message: 15/10/2012, 15h19
  4. Réponses: 4
    Dernier message: 08/07/2006, 06h36
  5. Comment connaitre les disques dur d'une machine dans un code ?
    Par GeekettePower dans le forum Langage
    Réponses: 3
    Dernier message: 27/06/2006, 00h43

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