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 :

[Swing] Dessin progressif d'un cercle : améliorations de mon code


Sujet :

AWT/Swing Java

  1. #1
    Membre chevronné
    Avatar de tails
    Homme Profil pro
    Inscrit en
    Novembre 2003
    Messages
    799
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations forums :
    Inscription : Novembre 2003
    Messages : 799
    Points : 2 148
    Points
    2 148
    Billets dans le blog
    15
    Par défaut [Swing] Dessin progressif d'un cercle : améliorations de mon code
    Bonjour, j'ai écrit une classe JFrame permettant, lorsque l'on appuie sur un bouton, d'animer le dessin d'un cercle plein rose sur fond jaune. Toutefois, j'ai besoin de votre aide sur quelques points.

    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
     
    package org.isgreat.loloof64.j2se.oval_progressive;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class OvalProgressive extends JFrame {
     
    	public static void main(String[] args) {
    		new OvalProgressive().setVisible(true);
    	}
     
    	public OvalProgressive() {
    		super("Oval Progressive application");
    		setSize(WIDTH, HEIGHT);
    		setLocationRelativeTo(CENTER_RELATIVE);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JPanel southPanel = new JPanel();
    		southPanel.add(startButton);
    		add(southPanel, BorderLayout.SOUTH);
     
    		startButton.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent actionEvent) {
    				new TimeSimulator().start();
    			}
     
    		});
     
    		computeOvalOffsets();
    	}
     
    	@Override
    	public void paint(Graphics graphics) {
    		super.paint(graphics);
    		graphics.setColor(BACKGROUND);
    		graphics.fillRect(0, 0, WIDTH, HEIGHT);
    		graphics.setColor(OVAL_COLOR);
    		graphics.fillArc(ovalOffsetX, ovalOffsetY, OVAL_DIAMETER, OVAL_DIAMETER,
    				START_ANGLE, (int) (-timePart*360) );
    	}
     
    	private void computeOvalOffsets(){
    		ovalOffsetX = ( getWidth() - OVAL_DIAMETER ) / 2;
    		ovalOffsetY = ( getHeight() - OVAL_DIAMETER ) / 2;
    	}
     
    	private void buttonStateBecomeStart() {
    		startButton.setText("Start");
    		startButton.setEnabled(true);
    	}
     
    	private void buttonStateBecomeWaiting() {
    		startButton.setText("Please Wait...");
    		startButton.setEnabled(false);
    	}
     
    	private class TimeSimulator extends Thread {
     
    		@Override
    		public void run() {
    			buttonStateBecomeWaiting();
    			while(timePart < 1.0f){
    				try {
    					Thread.sleep(DELAY_MILLIS);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				timePart += TIME_ATOMIC_INCREMENT;
    				repaint();
    			}
    			buttonStateBecomeStart();
    		}
     
    	}
     
    	private static final int WIDTH = 200;
    	private static final int HEIGHT = 230;
    	private static final int OVAL_DIAMETER = 130;
    	private static final int START_ANGLE = 0;
    	private static int ovalOffsetX;
    	private static int ovalOffsetY;
    	private static float timePart = 0;
    	private static final Component CENTER_RELATIVE = null;
    	private static final Color BACKGROUND = Color.YELLOW;
    	private static final Color OVAL_COLOR = Color.PINK;
    	private JButton startButton = new JButton("Start");
     
    	private static final long DELAY_MILLIS = 100;
    	private static final float TIME_ATOMIC_INCREMENT = 0.025f;
     
    	private static final long serialVersionUID = 1L;
    }
    Je voudrais hériter de vos conseils sur les points suivants :
    1) Le bouton disparait pendant l'animation du cercle
    2) Le flickering omniprésent
    3) Le bouton se situe dans un rectangle blanc à la fin de l'animation
    4) Pas moyen d'effacer le cercle affiché, à la fin de l'animation. La seule solution à laquelle j'ai pensée était buggué. Elle consistait à ajouter la section suivante à la fin de la méthode run() de la classe privée TimeSimulator, juste avant l'appel buttonStateBecomeStart(); :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Graphics graphics = getGraphics();
    graphics.fillRect(0, 0, WIDTH, HEIGHT);
    OvalProgressive.super.paint(graphics);

  2. #2
    Expert confirmé
    Avatar de slim_java
    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2008
    Messages
    2 272
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2008
    Messages : 2 272
    Points : 4 539
    Points
    4 539
    Par défaut
    Salut,
    je préfére ne pas faire le dessin du rectangle
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    graphics.fillRect(0, 0, WIDTH, HEIGHT);
    dans la méthode paint qui peut ralentir le dessin d’après ce que j'ai compris de tes questions.

  3. #3
    Membre chevronné
    Avatar de tails
    Homme Profil pro
    Inscrit en
    Novembre 2003
    Messages
    799
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations forums :
    Inscription : Novembre 2003
    Messages : 799
    Points : 2 148
    Points
    2 148
    Billets dans le blog
    15
    Par défaut J'ai du mal à trouver les bonnes méthodes
    Bonjour et merci beaucoup de m'avoir répondu
    Etant donné que je faisais dans le dessin personnalisé d'un composant, je me trompe toujours dans le choix des méthodes où je définis mes codes.

    J'ai en effet hésité entre paint(Graphics) et paintComponent(Graphics) : mais je je suis pas encore sûr que paintComponent(Graphics) changera grand chose.

    Je pense que j'aurais carrément dû utiliser un JPanel et rédéfinir la méthode paint(Graphics) (ce m'est venu à l'esprit au moment d'écrire ce message) : je pense qu'à force de programmer pour Android, je perds me repères en J2SE.

    En tous cas merci beaucoup

  4. #4
    Membre chevronné
    Avatar de tails
    Homme Profil pro
    Inscrit en
    Novembre 2003
    Messages
    799
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations forums :
    Inscription : Novembre 2003
    Messages : 799
    Points : 2 148
    Points
    2 148
    Billets dans le blog
    15
    Par défaut Ca y est :)
    Je confirme que pour obtenir le résultat attendu, il fallait bien utiliser un JPanel.
    Voici le code final, un peu tiré par les cheveux, je le reconnais :
    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
     
    package org.isgreat.loloof64.j2se.oval_progressive;
     
    import java.awt.BorderLayout;
     
    public class OvalProgressive extends JFrame {
     
    	public static void main(String[] args) {
    		new OvalProgressive().setVisible(true);
    	}
     
    	public OvalProgressive() {
    		super("Oval Progressive application");
    		setSize(WIDTH, HEIGHT);
    		setLocationRelativeTo(CENTER_RELATIVE);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		centerPanel = new OvalPanel();
    		JPanel southPanel = new JPanel();
    		southPanel.add(startButton);
    		add(centerPanel, BorderLayout.CENTER);
    		add(southPanel, BorderLayout.SOUTH);
     
    		startButton.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent actionEvent) {
    				new TimeSimulator().start();
    			}
     
    		});
     
    	}
     
    	private void resetOval() {
    		timePart = 0;
    	}
     
    	private void computeOvalOffsets(){
    		ovalOffsetX = ( centerPanel.getWidth() - OVAL_DIAMETER ) / 2;
    		ovalOffsetY = ( centerPanel.getHeight() - OVAL_DIAMETER ) / 2;
    	}
     
    	private void buttonStateBecomeStart() {
    		startButton.setText("Start");
    		startButton.setEnabled(true);
    	}
     
    	private void buttonStateBecomeWaiting() {
    		startButton.setText("Please Wait...");
    		startButton.setEnabled(false);
    	}
     
    	private class TimeSimulator extends Thread {
     
    		@Override
    		public void run() {
    			computeOvalOffsets();
    			buttonStateBecomeWaiting();
    			while(timePart < 1.0f){
    				try {
    					Thread.sleep(DELAY_MILLIS);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				timePart += TIME_ATOMIC_INCREMENT;
    				centerPanel.repaint();
    			}
    			resetOval();
    			buttonStateBecomeStart();
    		}
     
    	}
     
    	private class OvalPanel extends JPanel
    	{
     
    		@Override
    		public void paint(Graphics graphics) {
    			super.paint(graphics);
    			graphics.setColor(BACKGROUND);
    			graphics.fillRect(0, 0, getWidth(), getHeight());
    			graphics.setColor(OVAL_COLOR);
    			graphics.fillArc(ovalOffsetX, ovalOffsetY, OVAL_DIAMETER,
    					OVAL_DIAMETER, START_ANGLE, (int) (-timePart * 360));
    		}
     
    		private static final long serialVersionUID = 1L;
    	};
     
    	private static final int WIDTH = 200;
    	private static final int HEIGHT = 230;
    	private static final int OVAL_DIAMETER = 130;
    	private static final int START_ANGLE = 0;
    	private static int ovalOffsetX;
    	private static int ovalOffsetY;
    	private static float timePart = 0;
    	private static final Component CENTER_RELATIVE = null;
    	private static final Color BACKGROUND = Color.YELLOW;
    	private static final Color OVAL_COLOR = Color.PINK;
    	private JButton startButton = new JButton("Start");
    	private OvalPanel centerPanel;
     
    	private static final long DELAY_MILLIS = 100;
    	private static final float TIME_ATOMIC_INCREMENT = 0.025f;
     
    	private static final long serialVersionUID = 1L;
    }

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

Discussions similaires

  1. [SWING]Dessiner sur un composant
    Par Tarteens dans le forum AWT/Swing
    Réponses: 22
    Dernier message: 02/07/2007, 10h50
  2. Dessiner un arc de cercle autour d'une picturebox
    Par WindowsVista dans le forum VB.NET
    Réponses: 2
    Dernier message: 11/06/2007, 11h48
  3. [SWING][Dessin] Effet de répétition non désiré
    Par Invité dans le forum AWT/Swing
    Réponses: 18
    Dernier message: 11/12/2006, 10h23
  4. Réponses: 1
    Dernier message: 21/11/2006, 17h00
  5. [swing] Dessiner contour d'un composant
    Par issou dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 01/03/2006, 11h57

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