bonsoir,
je voudrai faire une animation ma frame contient une image .gif et deux buttons 'Démarrer'(pour bouger l'image)et 'Arrêter'(pour stopper l'animation ).Mais le probléme que lorsque je cliqué sur Arrêter l'annimation s'arret mais quand je clique sur Démarrer pour animer a nouveau l'animation s'arrive pas .
voila mon code :


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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
 
class pano extends JPanel
{
  public int posx=0;
  public void paintComponent(Graphics g)
  {
    g.setColor(Color.white);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
 
    try
    {
      Image img = ImageIO.read(new File("bato.gif"));
      g.drawImage(img, posx, 300, this);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
 
  public void setx(int xx)
  {
    posx=xx;
  }
}
 
 
class fenetre extends JFrame
{
  private JButton btn = new JButton("démarer");
  private JPanel pan = new JPanel();
  private pano o = new pano();
  private JButton btn2 = new JButton("Aréter");
  private JPanel pann = new JPanel();
  private boolean tt = true;
 
  public fenetre()
  {
    this.setLocation(100,100);
    this.setTitle("mon projet");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pan.setLayout(new BorderLayout());
    pan.add(o ,BorderLayout.CENTER);
    pann.add(btn);
    pann.add(btn2);
    pan.add(pann,BorderLayout.SOUTH);
    btn.addActionListener(new btt1());
    btn2.addActionListener(new btt2());
    this.setContentPane(pan);
    this.setSize(1500,900);
    this.setVisible(true);
    annimer();
  }
 
  public void annimer()
  {
    for(int i=0;i<700;i++)
    {
      if(this.tt==true)
      {
        int x = o.posx ;
        x++;
        o.setx(x);
        o.repaint();
 
        try
        {
          Thread.sleep(30);
        }
        catch (InterruptedException e)
        {
          e.printStackTrace();
        }
      }
      else
      {
        System.out.println("ffff");
      }
    }
  }
 
  class btt1 implements ActionListener
  {
    public void actionPerformed(ActionEvent arg0)
    {
      tt = true;
      btn.setEnabled(false);
      btn2.setEnabled(true);
      annimer();
    }
  }
 
  class btt2 implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      tt = false;
      btn.setEnabled(true);
      btn2.setEnabled(false);
      System.out.println("--- "+tt);
    }
  }
}
 
public class test
{
  public static void main(String [] args )
  {
    fenetre ya = new fenetre();
  }
}