
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@417 72836036-5685-4462-b002-a69064685172
64 lines
1.3 KiB
Java
64 lines
1.3 KiB
Java
package jrummikub.control;
|
|
|
|
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;
|
|
|
|
/**
|
|
* Count-down timer used to limit the turn time
|
|
*/
|
|
public class TurnTimer implements ActionListener, ITurnTimer {
|
|
private IView view;
|
|
private int timeLeft;
|
|
private int totalTime;
|
|
private Timer timer;
|
|
private Event timeRunOutEvent = new Event();
|
|
|
|
/**
|
|
* Create a new timer using a given view to display the current time left
|
|
*
|
|
* @param view
|
|
* view to display
|
|
* @param totalTime
|
|
* total time for turn
|
|
*/
|
|
public TurnTimer(IView view, int totalTime) {
|
|
this.view = view;
|
|
timeLeft = totalTime;
|
|
this.totalTime = totalTime;
|
|
timer = new Timer(1000, this);
|
|
timer.setRepeats(true);
|
|
timer.setCoalesce(false);
|
|
view.getPlayerPanel().setTime(timeLeft, totalTime);
|
|
}
|
|
|
|
@Override
|
|
public void startTimer() {
|
|
timer.start();
|
|
}
|
|
|
|
@Override
|
|
public void stopTimer() {
|
|
timer.stop();
|
|
}
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
timeLeft--;
|
|
view.getPlayerPanel().setTime(timeLeft, totalTime);
|
|
if (timeLeft == 0) {
|
|
timer.stop();
|
|
timeRunOutEvent.emit();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public IEvent getTimeRunOutEvent() {
|
|
return timeRunOutEvent;
|
|
}
|
|
}
|