Bonjour,
En quête d'info sur des tests, je bloque sur le code suivant.
Je voudrais que le dessin se déplace dans ma fenêtre.

J'initialise dans
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
public class Initialise_1 {
	public static void main(String[] args) {
 
		Interface_1 inter = new Interface_1("Interface");
 
		Thread thread = new calcul_1(inter);
		thread.run();
	}
}
cette classe calcul et temporise le déplacement
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
public class calcul_1 extends Thread {
 
	private Interface_1 inter1;
 
	calcul_1(Interface_1 inter) {
		this.inter1 = inter;
	}
 
	public void run() {
 
		int x = 0;
 
		while (true) {
			try {
 
				x = x + 1;
				System.out.println("x= " + x);
				inter1.setResultat1(x);
				Thread.sleep(1000); // tempo 1 secondes
 
			} catch (InterruptedException exception) {
			}
		}
	}
}
l'interface affiche la valeur du calcul. C'est à cet endroit que j'ai placé l'animation
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
 
public class Interface_1 extends JFrame {
 
	private static final long serialVersionUID = 1L;
	private JTextField text;
	private Image bufferedImage;
 
	Interface_1(String name) {
 
		super(name);
		setSize(new Dimension(400, 200));
 
		JPanel panel = new JPanel();
		FlowLayout flayout = new FlowLayout(FlowLayout.LEFT);
		panel.setLayout(flayout);
 
		JLabel label = new JLabel("x =");
		this.text = new JTextField();
		this.text.setPreferredSize(new Dimension(30, 20));
		this.text.setBackground(getForeground());
 
		panel.add(label);
		panel.add(this.text);
		setContentPane(panel);
		setVisible(true);
 
		// opération par défaut à la fermeture
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
	@SuppressWarnings("null")
	public void paint(Graphics g, final int resultat) {
		this.bufferedImage = new BufferedImage(10, 10,
				BufferedImage.TYPE_4BYTE_ABGR);
		((BufferedImage) this.bufferedImage).createGraphics();
 
		Graphics graph = null;
		graph.setColor(Color.black);
		graph.fillRect(0, 0, 10, 10);
 
		g.drawImage(this.bufferedImage, resultat, 30, 10, 10, this);
	}
 
	public void setResultat1(final int resultat) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				text.setText("" + resultat);
			}
		});
	}
}