📄 timer.java
字号:
package cn.pandaoen.game.minesweeper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import cn.pandaoen.widget.swt.LedDigit;
/**
* The instance of this class records the time
* after starting of a game.
*
* @author pan
*/
public class Timer extends Counter {
private long base;
private int offset;
private boolean running;
private Runnable runnable;
static final int PERIOD = 500;
public Timer(Composite parent, int style) {
super(parent, style);
final Display display = getDisplay();
runnable = new Runnable() {
public void run() {
if (isDisposed())
return;
long time = System.currentTimeMillis();
setValue(offset + (int) (time - base) / 1000);
display.timerExec(PERIOD, this);
}
};
}
public boolean isRunning() {
checkWidget();
return running;
}
public void reset() {
checkWidget();
base = System.currentTimeMillis();
offset = 0;
setValue(0);
}
public void start() {
checkWidget();
if (running)
return;
base = System.currentTimeMillis();
running = true;
Display display = getDisplay();
display.timerExec(0, runnable);
}
public void stop() {
checkWidget();
if (!running)
return;
offset = getValue();
running = false;
Display display = getDisplay();
display.timerExec(-1, runnable);
}
@Override
protected LedDigit getLedDigit() {
if (ledDigit == null) {
Display display = getDisplay();
ledDigit = new LedDigit(display, display
.getSystemColor(SWT.COLOR_GREEN), display
.getSystemColor(SWT.COLOR_BLACK), display
.getSystemColor(SWT.COLOR_DARK_GREEN));
}
return ledDigit;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -