bonjour,
voilà je développe une application autour de lancements de dés. une partie consiste en l'affichage progressif du résultat de lancement de dés. pour éviter de bloquer l'application à cause de l'EDT lors des affichages successifs j'utilise la classe SwingWorker.
Et là c'est le drame
mon appli a un comportement assez étrange. quand le nombres de dés est grand tout ce déroule comme prévu. mais dès que le nombre de dés est faible l'affichage se bloque et le nombres de cycles d'affichages est aléatoire (de 10 à une centaine).
Si quelqu'un à une idée je suis preneur.
voici mes classes:
la classe Stat contient entre autre un tableau stockant les du graphique.
le main:
la classe pour la frame:
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 public class TestThread { /** * @param args */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { /** Démarrage de l'interface graphique et du SwingWorker. **/ FrameTest frame = new FrameTest(); } }); } }
la classe pour dessiner dans le jpanel
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 package des.jet.appli.main.test; import java.awt.Color; import java.util.ArrayList; import java.util.Collection; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import des.jet.appli.ihm.sortie.onglet1.Dessin; import des.jet.appli.utils.business.Des; import des.jet.appli.utils.business.Stat; import des.jet.appli.utils.constantes.SortieOnglet1; @SuppressWarnings("serial") public class FrameTest extends JFrame implements SortieOnglet1{ private Dessin jDessin; private JButton start; private JButton exit; private JButton stop; private Collection<Des>cDes; private ThreadSwingWorker swingWorker; public FrameTest() { init(); ajoutButton(); } private void ajoutButton() { JPanel jPanelButtons = new JPanel(); this.getContentPane().add(jPanelButtons, "South"); start = new JButton("Start"); start.addActionListener(new GestionActionButton(this, 1)); jPanelButtons.add(start); stop = new JButton("Stop"); stop.addActionListener(new GestionActionButton(this, 2)); jPanelButtons.add(stop); exit = new JButton("Exit"); exit.addActionListener(new GestionActionButton(this, 3)); jPanelButtons.add(exit); } private void init() { /* Construction de l'interface graphique. */ this.setTitle("test Thread"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setBounds(400, 100, FRAME_RESULT_ONGLET1_WIDTH, FRAME_RESULT_ONGLET1_HEIGHT); jDessin = new Dessin(new Stat(), Color.RED); this.getContentPane().add(jDessin, "Center"); this.setVisible(true); // ajout des dés pour les tests cDes = new ArrayList<Des>(); cDes.add(new Des(9,5)); // cDes.add(new Des(5,6)); // cDes.add(new Des(15,10)); swingWorker = new ThreadSwingWorker(this); } public Dessin getJDessin() { return jDessin; } public void setJDessin(Dessin dessin) { jDessin = dessin; } public Collection<Des> getCDes() { return cDes; } public void setCDes(Collection<Des> des) { cDes = des; } public ThreadSwingWorker getSwingWorker() { return swingWorker; } public void setSwingWorker(ThreadSwingWorker swingWorker) { this.swingWorker = swingWorker; } }
la classe de thread:
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 package des.jet.appli.ihm.sortie.onglet1; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import des.jet.appli.utils.business.Stat; import des.jet.appli.utils.constantes.SortieOnglet1; @SuppressWarnings("serial") public class Dessin extends JPanel implements SortieOnglet1{ private Stat dataStat; private Color couleur; public Dessin(Stat dataStat, Color couleur) { this.dataStat = dataStat; this.couleur = couleur; this.setBounds(10, 10, 200, 100); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Rectangle R = new Rectangle(10, 10, FRAME_RESULT_ONGLET1_GRAPH_WIDTH + 10, FRAME_RESULT_ONGLET1_GRAPH_HEIGHT + 10, Color.blue,g); if(dataStat != null && dataStat.getLoiProba() != null && dataStat.getLoiProba().length != 0){ dessiner(g); } } public void dessiner(Graphics g) { int l, y, a; double h = FRAME_RESULT_ONGLET1_GRAPH_HEIGHT / dataStat.getValMax(); if(dataStat.getLoiProba().length < FRAME_RESULT_ONGLET1_GRAPH_WIDTH) { l = FRAME_RESULT_ONGLET1_GRAPH_WIDTH / dataStat.getLoiProba().length; a = 1; } else { l = 1; a = dataStat.getLoiProba().length / FRAME_RESULT_ONGLET1_GRAPH_WIDTH; } for(int x = 0; a * x < dataStat.getLoiProba().length; x++) { y = FRAME_RESULT_ONGLET1_GRAPH_HEIGHT - (int)(dataStat.getLoiProba()[a * x] * h); new Rectangle(x * l + 15, y + 15, l, (int)(dataStat.getLoiProba()[a * x] * h), couleur, g); } } public void redessiner(Stat dataStat, Color couleur){ this.dataStat = dataStat; this.couleur = couleur; Graphics g = getGraphics(); g.clearRect(10, 10, FRAME_RESULT_ONGLET1_GRAPH_WIDTH + 10, FRAME_RESULT_ONGLET1_GRAPH_HEIGHT + 10); dessiner(g); } public Stat getDataStat() { return dataStat; } public void setDataStat(Stat dataStat) { this.dataStat = dataStat; } }
la classe gérant les événements de boutons:
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 package des.jet.appli.main.test; import java.awt.Color; import java.util.List; import javax.swing.SwingWorker; import des.jet.appli.utils.business.Stat; import des.jet.appli.utils.calculs.CalculsProgressifsUtils; import des.jet.appli.utils.constantes.SortieOnglet1; class ThreadSwingWorker extends SwingWorker<Stat, Stat> implements SortieOnglet1{ private FrameTest frame; private int boucle = 1; private int processe = 1; private boolean bFinish = true; public ThreadSwingWorker(FrameTest frame){ this.frame = frame; } @Override public Stat doInBackground() { Stat stat = new Stat(); try { while(bFinish){ Thread.sleep(DEFAULT_LOOP_WAIT); stat = CalculsProgressifsUtils.getCalculsProgressifsUtils().boucleTirageAleatoire(stat, frame.getCDes(), 100); publish(stat); System.out.println("boucle n° : " + boucle); boucle++; if(boucle == DEFAULT_LOOP_NUMBER)bFinish = false; } } catch (InterruptedException e) { e.printStackTrace(); } return stat; } @Override protected void process(List<Stat> stats) { /** Mise à jour du JPanel de dessin **/ System.out.println("processe n° : " + processe + " nb param dans process : " + stats.size()); Stat stat = stats.get(0); stat.calculValMax(); frame.getJDessin().redessiner(stats.get(0), Color.RED); processe++; } @Override protected void done() { frame.setSwingWorker(new ThreadSwingWorker(frame)); } public boolean isBFinish() { return bFinish; } public void setBFinish(boolean finish) { bFinish = finish; } }
toutes vos remarques sont les bienvenues car là je sèche, l'appli réagissant à l'opposé de ce que l'on pourrait croire.
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 package des.jet.appli.main.test; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.SwingWorker; public class GestionActionButton implements ActionListener { private FrameTest frameTest; private int choix; public GestionActionButton(FrameTest frameTest, int choix) { this.frameTest = frameTest; this.choix = choix; } @Override public void actionPerformed(ActionEvent arg0) { if(choix == 1){ if(frameTest.getSwingWorker().getState() != SwingWorker.StateValue.STARTED){ frameTest.getSwingWorker().execute(); } } else if(choix == 2){ frameTest.getSwingWorker().setBFinish(false); System.out.println("Stop"); } else if(choix == 3){ System.exit(0); } } }
Partager