summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jrummikub/JRummikub.java4
-rw-r--r--src/jrummikub/control/TurnTimer.java44
2 files changed, 46 insertions, 2 deletions
diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java
index 5bddc68..5ea5a26 100644
--- a/src/jrummikub/JRummikub.java
+++ b/src/jrummikub/JRummikub.java
@@ -8,6 +8,7 @@ import java.util.Map;
import javax.swing.UIManager;
+import jrummikub.control.TurnTimer;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
@@ -33,7 +34,8 @@ public class JRummikub {
final IView view = new jrummikub.view.impl.View();
view.setCurrentPlayerName("Player 1");
- view.getPlayerPanel().setTimeLeft(42);
+ TurnTimer testTimer = new TurnTimer(view);
+ testTimer.startTimer();
view.getTablePanel().setLeftPlayerName("Player 2");
view.getTablePanel().setTopPlayerName("Player 3");
view.getTablePanel().setRightPlayerName("Player 4");
diff --git a/src/jrummikub/control/TurnTimer.java b/src/jrummikub/control/TurnTimer.java
index 701e892..9e694f7 100644
--- a/src/jrummikub/control/TurnTimer.java
+++ b/src/jrummikub/control/TurnTimer.java
@@ -1,5 +1,47 @@
package jrummikub.control;
-public class TurnTimer {
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import javax.swing.Timer;
+
+import jrummikub.util.Event;
+import jrummikub.util.IEvent;
+import jrummikub.view.IView;
+
+public class TurnTimer implements ActionListener {
+ private IView view;
+ private int timeLeft = 60;
+ private Timer timer;
+ private Event timeRunOutEvent = new Event();
+
+ public TurnTimer(IView view) {
+ this.view = view;
+ timer = new Timer(1000, this);
+ timer.setRepeats(true);
+ timer.setCoalesce(false);
+ view.getPlayerPanel().setTimeLeft(timeLeft);
+ }
+
+ public void startTimer() {
+ timer.start();
+ }
+
+ public void stopTimer() {
+ timer.stop();
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent arg0) {
+ timeLeft--;
+ view.getPlayerPanel().setTimeLeft(timeLeft);
+ if (timeLeft == 0) {
+ timer.stop();
+ timeRunOutEvent.emit();
+ }
+ }
+
+ public IEvent getTimeRunOutEvent() {
+ return timeRunOutEvent;
+ }
}