test commit

Timer
thomasmuller 2025-06-23 17:34:02 +02:00
parent 94bb556288
commit 5d45348c03
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package de.hs_mannheim.informatik.chess.controller;
import java.util.function.Consumer;
import javax.swing.Timer;
public class CountdownTimer {
private Timer timer;
private int secondsLeft;
private Runnable onTimeout;
private Consumer<Integer> onTick; // <- neue Callback-Funktion
public CountdownTimer(int minutes, int seconds) {
this.secondsLeft = minutes * 60 + seconds;
}
public void setOnTimeout(Runnable onTimeout) {
this.onTimeout = onTimeout;
}
public void setOnTick(Consumer<Integer> onTick) {
this.onTick = onTick;
}
public void start() {
if (timer != null) timer.stop();
timer = new Timer(1000, e -> {
secondsLeft--;
if (onTick != null) onTick.accept(secondsLeft); // <- tick callback
if (secondsLeft <= 0) {
stop();
if (onTimeout != null) onTimeout.run();
}
});
timer.start();
}
public void stop() {
if (timer != null) {
timer.stop();
}
}
public void reset(int minutes, int seconds) {
stop();
this.secondsLeft = minutes * 60 + seconds;
}
}