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
| public class CountDown extends JPanel implements Runnable{
private GregorianCalendar time;
private Timer timer;
private final GregorianCalendar FINISH = new GregorianCalendar(0, 0, 0, 0, 0, 0);
private final GregorianCalendar BEGIN_DELUGE = new GregorianCalendar(0, 0, 0, 0, 2, 0);
private int timeOfCountDown;
private static Font f = new Font("Book Antiqua", Font.BOLD, 25);
private int minutes;
private int secondes;
private boolean delugeTime = true;
public CountDown(int minutes, int secondes){
this.minutes = minutes;
this.secondes = secondes;
time = new GregorianCalendar(0, 0, 0, 0, minutes, secondes);
new Thread (this).run();
}
public void test(){
ActionListener reduceTime = new ActionListener() {
public void actionPerformed(ActionEvent e){
if( Model.isGameState() == 0 ){
attente();
}
else if ( Model.isGameState() == -1 | time.equals(FINISH)){timer.stop();}
else {time.add(Calendar.SECOND,-1);}
}
};
setTimeOfCountDown(transformCalendarToInt(new GregorianCalendar(0, 0, 0, 0, minutes, secondes)));
this.setOpaque(false);
this.setPreferredSize(new Dimension(72, 72));
timer = new Timer(1000, reduceTime);
timer.start();
}
public void attente(){
synchronized(this){
if(Model.isGameState() == 0){
System.out.println("On attend tjrs");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void reprise(){
synchronized(this){
if(Model.isGameState() == 1){
System.out.println("Reprise du thread");
this.notifyAll();
}
}
}
public void run() {
test();
} |
Partager