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

AWT/Swing Java Discussion :

Animer un JLayeredPane et convertir la valeur de "pos" avec DecimalFormat


Sujet :

AWT/Swing Java

  1. #1
    Expert confirmé Avatar de Toufik83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2012
    Messages
    2 486
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 2 486
    Points : 5 052
    Points
    5 052
    Par défaut Animer un JLayeredPane et convertir la valeur de "pos" avec DecimalFormat
    Bonjour,

    Je souhaite convertir la valeur de "pos" de la méthode setComponentConstraints() avec la classe DecimalFormat, mais j'ai une erreur à la ligne 48 de Login.java, auriez vous une idée?
    Code java : 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
     
    private final DecimalFormat df=new DecimalFormat("##0.###");
    ....
    TimingTarget target = new TimingTargetAdapter(){
                @Override
                public void timingEvent(float fraction){
                    double fractionCover;
                    if(isLogin) fractionCover = 1f - fraction;
                    else fractionCover=fraction;
     
                    fractionCover=Double.parseDouble(df.format(fractionCover));//Ligne 48 Login.java erreur ici
     
                    mig.setComponentConstraints(container, "width "+coverSize+"%, pos "+fractionCover+"al 0 n 100%");
                    bg.revalidate();
                }
                @Override
                public void end(){
                    isLogin=!isLogin;
                }
            };
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0,001"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:651)
    at interfaces.Login$1.timingEvent(Login.java:48)
    at org.jdesktop.animation.timing.Animator.timingEvent(Animator.java:728)
    at org.jdesktop.animation.timing.Animator.access$200(Animator.java:75)
    at org.jdesktop.animation.timing.Animator$TimerTarget.actionPerformed(Animator.java:1041)
    at java.desktop/javax.swing.Timer.fireActionPerformed(Timer.java:311)
    at java.desktop/javax.swing.Timer$DoPostEvent.run(Timer.java:243)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

  2. #2
    Expert confirmé Avatar de Toufik83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2012
    Messages
    2 486
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 2 486
    Points : 5 052
    Points
    5 052
    Par défaut
    Bonjour,

    J'ai trouvé une solution pour contourner le problème, mais je n'ai toujours pas compris ce qui se passe avec la valeur de fraction.

    J'ai d'abord modifier le pattern de la class DecimalFormat en supprimant le dernier diez # DecimalFormat("##0.##"), et utilisé des System.out.println() avant et après la conversion en double pour voir les valeurs de fraction et fractionCover, et j'ai mis la partie de code dans un bloc try catch comme suite
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    double fractionCover;
    if(isLogin) fractionCover = 1f - fraction;
    else fractionCover=fraction;
    System.out.println("fraction :"+fraction+", fractionCover before :"+fractionCover);
    try{
         fractionCover=Double.parseDouble(df.format(fractionCover));
    }catch(NumberFormatException e){
         System.err.println("Parse Error...FC :"+fractionCover);
    }
    System.out.println("fractionCover after :"+fractionCover);
    mig.setComponentConstraints(container, "width "+coverSize+"%, pos "+fractionCover+"al 0 n 100%");
    bg.revalidate();

    Lorsque la valeur est 0.0 ou 1.0 l'erreur dans le bloc catch ne s'affiche pas, par contre, elle apparait toujours dans l'intervalle 0<valeur<=1

    Voilà ce que la console affiche
    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
    fraction :0.0033620002, fractionCover before :0.003362000221386552
    fractionCover after :0.0
    fraction :0.005618, fractionCover before :0.005617999937385321
    Parse Error...FC :0.005617999937385321
    fractionCover after :0.005617999937385321
    ...
    fraction :0.993502, fractionCover before :0.9935020208358765
    Parse Error...FC :0.9935020208358765
    fractionCover after :0.9935020208358765
    fraction :0.996638, fractionCover before :0.9966380000114441
    fractionCover after :1.0
    fraction :0.99875, fractionCover before :0.9987499713897705
    fractionCover after :1.0
    fraction :0.999838, fractionCover before :0.9998379945755005
    fractionCover after :1.0
    fraction :0.999902, fractionCover before :0.9999020099639893
    fractionCover after :1.0
    fraction :0.999968, fractionCover before :0.9999679923057556
    fractionCover after :1.0
    fraction :1.0, fractionCover before :1.0
    fractionCover after :1.0
    L'animation fonctionne comme je veux malgré les messages d'erreur, mais j'essaies quand même de comprendre surtout ce qui se passe et comment faire pour éviter le try catch.

  3. #3
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 087
    Points : 8 006
    Points
    8 006
    Par défaut
    Ce n'est pas un probleme de . et de , en fonction de la Locale ?
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  4. #4
    Expert confirmé Avatar de Toufik83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2012
    Messages
    2 486
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 2 486
    Points : 5 052
    Points
    5 052
    Par défaut
    Bonjour,

    Merci wax78 d'avoir pris le temps de me répondre, la réponse à votre question est non, car j'ai une erreur Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Malformed pattern "##0,##" quand je remplace le point du pattern par une virgule.

    Apparemment le pattern n'accepte pas les virgules.

    Edit :

    Vous aviez raison wax78, c'est moi qui n'avais pas compris votre réponse

    La bonne syntaxe pour instancier DecimalFormat est new DecimalFormat("##0.###", DecimalFormatSymbols.getInstance(Locale.US));.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Convertir double en char* avec une precision full range
    Par Boris Jovanovic dans le forum C
    Réponses: 4
    Dernier message: 28/11/2018, 19h14
  2. Réponses: 9
    Dernier message: 19/04/2013, 17h46
  3. convertir double en int64
    Par akrlot dans le forum C
    Réponses: 15
    Dernier message: 28/06/2007, 14h59
  4. Réponses: 2
    Dernier message: 30/08/2006, 16h08
  5. Problème avec DecimalFormat
    Par g0ldenrno dans le forum Langage
    Réponses: 10
    Dernier message: 20/06/2006, 19h45

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