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 :

Animation en Java


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 15
    Par défaut Animation en Java
    Salut tout le monde

    Je veux faire clignoter un jLabel sous netBeans. J'ai essayé le code suivat:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    public class Interface1 extends javax.swing.JFrame {
     
        /** Creates new form Interface1 */
        public Interface1() {
            initComponents(); 
     
            JLabel   lab = new logiciel.BlinkingLabel("GestOp", Color.CYAN);
     
        }
    La classe BlinkingLabel se trouve dans un package nommé " logiciel" :
    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
     
     
    package logiciel;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class BlinkingLabel extends JLabel
    {  
      Color defaultColor;   // Couleur par defaut du label
      Color blinkingColor;  // Couleur de clignotement
      Timer timer;          // Timer declenchant des tics
     
      public BlinkingLabel (String text,  Color  blinkingColor) 
      {	
        super (text);    
        this.blinkingColor = blinkingColor;
        this.defaultColor  = getForeground ();
     
        // Creation et lancement du timer
        timer = createTimer ();
        timer.start ();
      }
     
      // Methode renvoyant un timer pret a demarrer
      private Timer createTimer ()
      {
        // Creation d'une instance de listener associee au timer
        ActionListener action = new ActionListener ()
          {
            // Methode appelee a chaque tic du timer
            public void actionPerformed (ActionEvent event)
            {
              // Inversion de la couleur
              if (getForeground ().equals (defaultColor))            
                setForeground (blinkingColor);
              else
                setForeground (defaultColor);
            }
          };
     
        // Creation d'un timer qui genere un tic
        // chaque 500 millieme de seconde
        return new Timer (500, action);
      }
    Ça ne marche pas . please help

  2. #2
    Membre éclairé
    Avatar de omar344
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2007
    Messages
    287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2007
    Messages : 287
    Par défaut
    Bonjour!
    Voici un exemple fonctionnel:
    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
    import java.util.Timer;
    import java.awt.Color;
    import java.util.TimerTask;
    import javax.swing.JLabel;
     
    /**
     * A blinkable label.
     * This class adds blinking behavior to a JLabel.
     * Use setBlinking(true) to turn blinking on.
     * 
     * How it works:
     * 1. it uses a Timer and TimerTask to perform blinking.
     * 2. the TimerTask toggles the foreground color between the
     *    original foreground color and the background color.  This makes 
     *    the label text appear and disappear (when foreground == background).
     * 
     * TODO: Make this a decorator. Then you could blink any Component.
     * 
     * @author James Brucker
     */
    public class BlinkingLabel extends JLabel {
    	private static final long serialVersionUID = 1L;
    	private static final int BLINKING_RATE = 1000; // period in milliseconds
     
    	private boolean blinking = false;
    	private Timer timer = null;
     
    	/**
             * Create a new blinkable label with given text string.
             * @param text a string to display on the label.
             */
    	public BlinkingLabel(String text) {
    		super(text);	
    	}
     
    	/**
             * Turn the blinking on or off.
             * @param blinking is true to turn blinking on
             */
    	public void setBlinking(boolean blinking) {
    		if ( this.blinking == blinking ) return; // nothing to do
    		this.blinking = blinking;
    		// if there is a timer running then cancel it.
    		// there should only be a timer if the label is blinking.
    		if ( timer != null ) {
    			timer.cancel();
    			timer = null;
    		}
    		// to make the label blink, schedule a BlinkTask.
    		if ( blinking ) {
    			timer = new Timer();
    			// parameters are: (TimerTask, delay, period)
    			timer.scheduleAtFixedRate(new BlinkTask(this), 0, BLINKING_RATE);	
    		}
    	}
     
    	/**
             * Get the blinking state of the label.
             * @return true if label is in blinking state
             */
    	public boolean getBlinking( ) {
    		return this.blinking;
    	}
     
    	/**
             * A task to control blinking behavior.
             */
    	class BlinkTask extends TimerTask {
    		private JLabel label;
    		/** label's original background color */
    		private Color bg;
    		/** label's original foreground color */
    		private Color fg;
     
    		public BlinkTask(JLabel label) {
    			this.label = label;
    			fg = label.getForeground();
    			bg = label.getBackground();
    		}
     
    		/**
                     * Reverse the foreground color of label.
                     */
                    @Override
    		public void run( ) {
    			if ( label.getForeground() == fg )
    				label.setForeground(bg);
    			else label.setForeground(fg);
    		}
     
    		/**
                     * When the TimerTask is canceled, restore the original
                     * foreground color.  This ensures the label is visible.
                     */
                    @Override
    		public boolean cancel() {
    			label.setForeground(fg);
    			return true; // success
    		}
    	}
    }

  3. #3
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 15
    Par défaut
    Merci pour réponse
    J’ai essayé avec ce code main j’ai toujours le même problème. I.e. Quand je fais un appel à la classe BlinkinkLabel dans une autre class ça ne fonctionne plus :/

  4. #4
    Membre éclairé
    Avatar de omar344
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2007
    Messages
    287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2007
    Messages : 287
    Par défaut
    Bonsoir!
    ce code est fonctionnel, n'oublie pas l'appel de la méthode setBlinking pour avoir le résultat souhaité

  5. #5
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 15
    Par défaut
    C'est ce que j'ai fais :/


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     
             public Interface1() {
            initComponents(); 
            BlinkingLabel lab1 = new BlinkingLabel("GestOP");
            lab1.setBlinking(true);
            this.add(lab1);
     
     
        }

  6. #6
    Membre éclairé
    Avatar de omar344
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2007
    Messages
    287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2007
    Messages : 287
    Par défaut
    En principe ca devrait marcher!

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

Discussions similaires

  1. Animation en java
    Par Mo_Poly dans le forum 2D
    Réponses: 5
    Dernier message: 01/03/2007, 18h04
  2. Animation sous Java
    Par f2001 dans le forum Applets
    Réponses: 13
    Dernier message: 19/08/2006, 19h05
  3. animation en java
    Par mwanjany dans le forum Langage
    Réponses: 3
    Dernier message: 10/04/2006, 13h20
  4. Afficher un gif animé en Java
    Par julio26 dans le forum 2D
    Réponses: 5
    Dernier message: 06/03/2006, 12h04
  5. [Stratégie]Boucle d'animation en Java
    Par Invité dans le forum Graphisme
    Réponses: 10
    Dernier message: 01/02/2005, 19h49

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