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 :

Chronomètre en JavaFX


Sujet :

JavaFX

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Fonctionnaire
    Inscrit en
    Août 2016
    Messages
    28
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Fonctionnaire
    Secteur : Finance

    Informations forums :
    Inscription : Août 2016
    Messages : 28
    Par défaut Chronomètre en JavaFX
    Bonjour,
    Je souhaite créer un chronomètre en JavaFX. J'ai pu récupérer le code d'un chronomètre en Swing.
    J'essaye de modifier le code pour le faire tourner en JavaFX mais j'ai une erreur à la méthode super.paintComponent(g); qui est appelée (je pense) par repaint(); :
    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
     
    public void run()
        {
            while(on)
            {
                pause(9);
     
                if (c<99) c+=1;
                else{
                    c = 0;
                    if (s<59) s+=1;
                    else{
                        s = 0;
                        if (m<59) m+=1;
                        else{
                            m = 0;
                            h+=1;
                        }
                    }
                }
     
                if (! on) c--;//pb de synchronisation
     
    //            time = setTime(h,m,s,c);
                time = setTime(h,m,s);
                lcd = setLCD();
     
                repaint();
            }
        }
     
        public void paintComponent(GraphicsContext g)
        {
            super.paintComponent(g);
     
            lcd[0].draw(g,10,10);
            lcd[1].draw(g,22,10);
            lcd[2].draw(g,42,10);
            lcd[3].draw(g,54,10);
            lcd[4].draw(g,74,10);
            lcd[5].draw(g,86,10);
    //        lcd[6].draw(g,106,10);
    //        lcd[7].draw(g,118,10);
        }
    Est-ce que quelqu'un à une idée pour adapter çà en JavaFX ?

    Je vous remercie d'avance.

    Marc

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    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 900
    Billets dans le blog
    54
    Par défaut
    A moins que tu ne bosses avec un Canvas et un FXGraphics2D de JFree ou que tu n'inclue le composants Swing dans une interface FX tu peux tout de suite oublier le contenu de ton repainComponent().

    Alors dis-nous, il ressemble a quoi ton chronomètre (apparemment un affichage de nombres à cristaux liquides plutôt qu'une montre d’après le code) et surtout, il est constitue de nœuds vectoriels ou d'un dessin bitmap ???
    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

  3. #3
    Membre averti
    Homme Profil pro
    Fonctionnaire
    Inscrit en
    Août 2016
    Messages
    28
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Fonctionnaire
    Secteur : Finance

    Informations forums :
    Inscription : Août 2016
    Messages : 28
    Par défaut
    Bonjour et merci.

    Oui, j'ai essayé de 'jouer' avec Canvas et FXGraphics2D mais sans succès !
    Voici :
    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
     
    class Chrono1 extends JPanel implements ActionListener, Runnable{
        Font f;
        Color bd,lt,dk;
        int h,m,s,c;
        String time;
        Thread runner;
        JButton start,stop,init;
        JPanel p;
        boolean on;
        LcdDigit[] lcd;
     
        public Chrono1(){
            this(0,0,0);
        }
     
        public Chrono1(int he,int mn, int se){
            super(new BorderLayout());
     
            h = he;
            m = 01;
            s = 05;
     
            p = new JPanel(new FlowLayout());
            start = new JButton("start");
            start.addActionListener(this);
            stop = new JButton("stop");
            stop.addActionListener(this);
            init = new JButton("init");
            init.addActionListener(this);
            p.add(init);
            p.add(start);
            p.add(stop);
            add("South",p);
     
            f = new Font("TimesRoman",Font.BOLD,20);
            setFont(f);
     
            lt = Color.yellow;
            dk = Color.darkGray;
            bd = Color.black;
     
            setBackground(bd);
     
     
            time = "00:00:00";
            lcd = setLCD();
     
            runner = null;
            on = false;
        }
     
        private String setTime(int x, int y, int z)
        {
            String s1,s2,s3;
            if (h<10) s1 = "0"+h;
            else s1 = ""+h;
            if (m<10) s2 = ":0"+m;
            else s2 = ":"+m;
            if (s<10) s3 = ":0"+s;
            else s3 = ":"+s;
            return s1+s2+s3;
        }
     
     
        public void pause(int time)
        {
            try{ Thread.sleep(time);}
            catch(InterruptedException e){}
        }
     
        public void run()
        {
            while(on)
            {
                pause(9);
     
                if (c<99) c+=1;
                else{
                    c = 0;
                    if (s<59) s+=1;
                    else{
                        s = 0;
                        if (m<59) m+=1;
                        else{
                            m = 0;
                            h+=1;
                        }
                    }
                }
     
                if (! on) c--;//pb de synchronisation
     
                time = setTime(h,m,s);
                lcd = setLCD();
     
                repaint();
            }
        }
     
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
     
            lcd[0].draw(g,10,10);
            lcd[1].draw(g,22,10);
            lcd[2].draw(g,42,10);
            lcd[3].draw(g,54,10);
            lcd[4].draw(g,74,10);
            lcd[5].draw(g,86,10);
        }
     
        public void actionPerformed(ActionEvent e){
            String ch = e.getActionCommand();
            if (ch.equals("start")){
                on = true;
                if (runner==null){
                    runner = new Thread(this);
                    runner.start();
                }
            }
            else if (ch.equals("stop")) {
                on = false;
                runner = null;
            }
            else {
                runner = null;
                on = false;
                h = m = s = c = 0;
                time = setTime(0,0,0);
                lcd = setLCD();
                repaint();
            }
        }
     
        public void stop(){
            runner = null;
        }
     
        public LcdDigit[] setLCD(){
            LcdDigit[] lc = new LcdDigit[6];
            lc[0] = new LcdDigit(Integer.parseInt(time.substring(0,1)),10,20,lt,dk);
            lc[1] = new LcdDigit(Integer.parseInt(time.substring(1,2)),10,20,lt,dk);
            lc[2] = new LcdDigit(Integer.parseInt(time.substring(3,4)),10,20,lt,dk);
            lc[3] = new LcdDigit(Integer.parseInt(time.substring(4,5)),10,20,lt,dk);
            lc[4] = new LcdDigit(Integer.parseInt(time.substring(6,7)),10,20,lt,dk);
            lc[5] = new LcdDigit(Integer.parseInt(time.substring(7,8)),10,20,lt,dk);
            return lc;
        }
     
    }
    et :
    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
     
    class LcdDigit {
     //--------------------------------------------------------------------Constants
     private static int atMapX[][] = {{1,1,1,1,1,1,0},  // Indicates which segments
                                      {0,0,0,0,1,1,0},  // are lighted to draw each
                                      {1,0,1,1,0,1,1},  // digit.
                                      {1,0,0,1,1,1,1},
                                      {0,1,0,0,1,1,1},
                                      {1,1,0,1,1,0,1},
                                      {1,1,1,1,1,0,1},
                                      {1,0,0,0,1,1,0},
                                      {1,1,1,1,1,1,1},
                                      {1,1,0,1,1,1,1}};
     //-------------------------------------------------------------------Attributes
     private int   atDigit;      // Number of the digit.
     private int   atWidth;      // Width of the digit.
     private int   atHeight;     // Height of the digit.
     private Color atLightColor; // Color of the lightened segments.
     private Color atDarkColor;  // Color of the darkened segments.
     //------------------------------------------------------------------Constructor
     // Creates a digit.
     public LcdDigit(int agDigit,int agWidth,int agHeight,Color agLightColor,
                     Color agDarkColor) {
      atDigit=agDigit;
      atWidth=agWidth;
      atHeight=agHeight;
      atLightColor=agLightColor;
      atDarkColor=agDarkColor;
     }
     
     // Draws the digit at a given position.
     public void draw(Graphics agGraphic,int agPositionX,int agPositionY) {
      int lcSegmentXS[] = new int [7];
      int lcSegmentYS[] = new int [7];
     
      // Numbering of the segments:
      //
      //  ##0##
      // #     #
      // 1     5
      // #     #
      //  ##6##
      // #     #
      // 2     4
      // #     #
      //  ##3##
      //
     
      // Segment No 0.
      if (atMapX[atDigit][0]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX+(int)(atWidth*0.1);
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.9);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.7);
      lcSegmentXS[3]=agPositionX+(int)(atWidth*0.3);
      lcSegmentXS[4]=agPositionX+(int)(atWidth*0.1);
     
      lcSegmentYS[0]=agPositionY;
      lcSegmentYS[1]=agPositionY;
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.1);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.1);
      lcSegmentYS[4]=agPositionY;
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,5);
     
      // Segment No 1.
      if (atMapX[atDigit][1]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX;
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.2);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.2);
      lcSegmentXS[3]=agPositionX;
      lcSegmentXS[4]=agPositionX;
     
      lcSegmentYS[0]=agPositionY+(int)(atHeight*0.05);
      lcSegmentYS[1]=agPositionY+(int)(atHeight*0.15);
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.35);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.45);
      lcSegmentYS[4]=agPositionY+(int)(atHeight*0.05);
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,5);
     
      // Segment No 2.
      if (atMapX[atDigit][2]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX;
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.2);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.2);
      lcSegmentXS[3]=agPositionX;
      lcSegmentXS[4]=agPositionX;
     
      lcSegmentYS[0]=agPositionY+(int)(atHeight*0.55);
      lcSegmentYS[1]=agPositionY+(int)(atHeight*0.65);
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.85);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.95);
      lcSegmentYS[4]=agPositionY+(int)(atHeight*0.55);
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,5);
     
      // Segment No 3.
      if (atMapX[atDigit][3]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX+(int)(atWidth*0.1);
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.9);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.7);
      lcSegmentXS[3]=agPositionX+(int)(atWidth*0.3);
      lcSegmentXS[4]=agPositionX+(int)(atWidth*0.1);
     
      lcSegmentYS[0]=agPositionY+atHeight;
      lcSegmentYS[1]=agPositionY+atHeight;
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.9);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.9);
      lcSegmentYS[4]=agPositionY+atHeight;
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,5);
     
      // Segment No 4.
      if (atMapX[atDigit][4]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX+atWidth;
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.8);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.8);
      lcSegmentXS[3]=agPositionX+atWidth;
      lcSegmentXS[4]=agPositionX+atWidth;
     
      lcSegmentYS[0]=agPositionY+(int)(atHeight*0.55);
      lcSegmentYS[1]=agPositionY+(int)(atHeight*0.65);
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.85);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.95);
      lcSegmentYS[4]=agPositionY+(int)(atHeight*0.55);
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,5);
     
      // Segment No 5.
      if (atMapX[atDigit][5]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX+atWidth;
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.8);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.8);
      lcSegmentXS[3]=agPositionX+atWidth;
      lcSegmentXS[4]=agPositionX+atWidth;
     
      lcSegmentYS[0]=agPositionY+(int)(atHeight*0.05);
      lcSegmentYS[1]=agPositionY+(int)(atHeight*0.15);
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.35);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.45);
      lcSegmentYS[4]=agPositionY+(int)(atHeight*0.05);
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,5);
     
      // Segment No 6.
      if (atMapX[atDigit][6]==1) agGraphic.setColor(atLightColor);
      else agGraphic.setColor(atDarkColor);
     
      lcSegmentXS[0]=agPositionX+(int)(atWidth*0.1);
      lcSegmentXS[1]=agPositionX+(int)(atWidth*0.2);
      lcSegmentXS[2]=agPositionX+(int)(atWidth*0.8);
      lcSegmentXS[3]=agPositionX+(int)(atWidth*0.9);
      lcSegmentXS[4]=agPositionX+(int)(atWidth*0.8);
      lcSegmentXS[5]=agPositionX+(int)(atWidth*0.2);
      lcSegmentXS[6]=agPositionX+(int)(atWidth*0.1);
     
      lcSegmentYS[0]=agPositionY+(int)(atHeight*0.5);
      lcSegmentYS[1]=agPositionY+(int)(atHeight*0.45);
      lcSegmentYS[2]=agPositionY+(int)(atHeight*0.45);
      lcSegmentYS[3]=agPositionY+(int)(atHeight*0.5);
      lcSegmentYS[4]=agPositionY+(int)(atHeight*0.55);
      lcSegmentYS[5]=agPositionY+(int)(atHeight*0.55);
      lcSegmentYS[6]=agPositionY+(int)(atHeight*0.5);
     
      agGraphic.fillPolygon(lcSegmentXS,lcSegmentYS,7);
     }
    }

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    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 900
    Billets dans le blog
    54
    Par défaut
    Pour une réutilisation tel quel, il suffit de l'embarquer dans un SwingNode :

    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
    public final class ChronoApp extends Application {
     
        private Chrono1 chrono;
     
        @Override
        public void start(final Stage primaryStage) throws Exception {
            final SwingNode swingNode = new SwingNode();
            final StackPane root = new StackPane(swingNode);
            final Scene scene = new Scene(root);
            SwingUtilities.invokeLater(() -> {
                chrono = new Chrono1();
                swingNode.setContent(chrono);
            });
            primaryStage.setTitle("Chrono");
            primaryStage.setOnCloseRequest(windowEvent -> {
                chrono.stop();
                swingNode.setContent(null);
                Platform.runLater(Platform::exit);
                // Note : si le chrono est en train de tourner l'application ne s'arrete pas.
                Platform.runLater(() -> System.exit(0));
            });
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }
    Je ne suis pas tout a fait fan de la manière dont tu arrêtes ton thread, c'est a dire en le mettant a null en espérant que le GC le réclame juste après. Ça veut dire que s'il n'y a pas de GC ou s'il est lent ou occupe, le chrono continuera de fonctionner. Pour être un peu plus juste il faudrait invoquer runner.interrupt() et verifier Thread.currentThread().isInterrupted() dans la boucle. En plus ta méthode stop() ne modifie pas la valeur de on.

    Enfin même ainsi, des fois le programme continue de tourner quand on ferme la fenêtre lorsque le chrono est actif, d’où le fait que j'ai fini par rajouter un appel a System.exit().

    Pour le reste, tout recoder en FX avec des Path + CSS pour faire une version vectorielle, avec Canvas pour du bitmap ou FXGraphics2D pour conserver des appels a Java2D.
    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. Comment réaliser un chronomètre ?
    Par theprimitive dans le forum Langage
    Réponses: 18
    Dernier message: 25/10/2005, 08h59
  2. [C++ Builder 6] Timer de précision pour chronomètre
    Par doudoustephane dans le forum C++Builder
    Réponses: 9
    Dernier message: 27/09/2005, 10h45
  3. Comment faire un chronomètre en Delphi ou ASM ?
    Par PoOky dans le forum Langage
    Réponses: 8
    Dernier message: 15/06/2005, 20h49
  4. Comment chronométrer une fonction
    Par 323 dans le forum Pascal
    Réponses: 3
    Dernier message: 19/03/2003, 20h24
  5. [MFC] Boîte de dialogue chronométrée
    Par mdriesbach dans le forum MFC
    Réponses: 5
    Dernier message: 18/02/2003, 12h40

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