Bonjour,

Je suis actuellement en train de développer un petit jeu en Java et je rencontre un problème (ou plutôt je ne sais pas comment faire... ).
Le principe est que le joueur doit cliquer sur un bouton le plus de fois possible en 10 secondes, puis après cela s'arrête. Ensuite si le nombre de clics est supérieur à 50 le défi est gagné sinon c'est perdu.

Cependant, je ne vois pas comment réaliser le timer, et comment celui-ci, une fois fini (au bout des dix secondes du coup), pourrait bloquer le nombre de clics.

Voici les deux classes du mini-programme :

Classe MainSpamit :
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
 
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
@SuppressWarnings("serial")
class AfficheImage extends JPanel {
	Image SpamIt;
 
	AfficheImage(String s) {
		SpamIt = getToolkit().getImage(s);
	}
 
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(SpamIt, 0, 0, getWidth(), getHeight(), this);
	}
}
 
@SuppressWarnings("serial")
class Click extends JFrame {
	JLabel labelAttempt;
 
	public Click() {
 
		setTitle("SPAM IT");
		setContentPane(new AfficheImage("G:\\SpamIt.png"));
		setSize(920, 600);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container mainContainer = getContentPane();
		mainContainer.setLayout(null);
 
		JLabel labelTimer = new JLabel("TIMER HERE ");
		labelTimer.setFont(new Font("Verdana", 1, 10));
		labelTimer.setForeground(Color.WHITE);
		setLocationRelativeTo(null);
		labelTimer.setSize(500, 500);
		labelTimer.setLocation(10, 300);
 
		labelAttempt = new JLabel("Cliquez ci-dessous pour démarrer");
		labelAttempt.setFont(new Font("Verdana", 1, 10));
		labelAttempt.setForeground(Color.WHITE);
		labelAttempt.setSize(200, 20);
		labelAttempt.setLocation(370, 200);
 
		JButton button = new ClickCounterSpamIt();
		button.setSize(350, 100);
		button.setLocation(275, 235);
 
		mainContainer.add(labelTimer);
		mainContainer.add(labelAttempt);
		mainContainer.add(button);
 
	}
 
	public static void main(String[] args) {
		Click Game = new Click();
	}
}
Classe ClickCounterSpamIt :
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
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
 
@SuppressWarnings({ "serial", "restriction" })
public class ClickCounterSpamIt extends JButton implements ActionListener {
 
	private int nbClicks;
 
	public ClickCounterSpamIt() {
		this.nbClicks = 0;
		this.addActionListener(this);
	}
 
	@Override
	public void actionPerformed(ActionEvent event) {
		this.nbClicks++;
		this.setText("Nombre de clics: " + nbClicks);
	}
}
Pourriez vous m'éclairer sur ce sujet ?
Merci par avance