-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.java~
126 lines (108 loc) · 3.86 KB
/
Timer.java~
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*----------------------------------------------------------------
* Author: K. Walsh
* Email: kwalsh@holycross.edu
* Written: 7/13/2015
*
* Timer keeps track of elapsed time.
*----------------------------------------------------------------*/
import GUI.*;
/**
* A <i>Timer</i> object keeps track of elapsed time. It can be started,
* stopped, or reset. A Timer knows how to draw itself on a Canvas.
*/
public class Timer extends GUI.Widget {
/**
* The width of the box as it is shown on the screen.
*/
public static final int WIDTH = 150;
/**
* The height of the box as it is shown on the screen.
*/
public static final int HEIGHT = 75;
// Whether we are currently counting or not.
private boolean isCounting;
// Time of day at which we started counting.
private double startTime;
// Number of seconds elapsed so far.
private double elapsedSeconds;
/**
* Initialize a new timer.
* @param x the x coordinate of the location to draw the timer.
* @param y the y coordinate of the location to draw the timer.
*/
public Timer(int x, int y) {
super(x, y, WIDTH, HEIGHT);
isCounting = false;
startTime = 0;
elapsedSeconds = 0;
}
/**
* Start the timer.
*/
public void startCounting() {
// If already counting, do nothing.
if (isCounting)
return;
// We subtract the previously elapsed seconds in order
// to get a total cumulative elapsed time, even if the
// timer is stopped then restarted.
startTime = System.currentTimeMillis()/1000.0 - elapsedSeconds;
isCounting = true;
}
/**
* Stop the timer.
*/
public void stopCounting() {
isCounting = false;
elapsedSeconds = System.currentTimeMillis()/1000.0 - startTime;
}
/**
* Reset the timer back to time n.
* If the timer is counting, it will remain so.
* @param n the number of seconds to set the timer to.
*/
public void reset(double n) {
elapsedSeconds = n;
if (isCounting)
startTime = System.currentTimeMillis()/1000.0;
}
/**
* Get the time (in seconds) that has elapsed while the timer was
* counting. The time is cumulative, even if the timer is stopped then
* restarted.
*/
public int getElapsedSeconds() {
// Update the elapsed time, if needed, then return it.
if (isCounting)
elapsedSeconds = System.currentTimeMillis()/1000.0 - startTime;
return (int)elapsedSeconds;
}
/**
* Draw the timer box on a canvas. Don't call this directly, it is called by
* the GUI system automatically. This function should draw something on the
* canvas. Usually the drawing should stay within the bounds (x, y, width,
* height) which are protected member variables of GUI.Widget, which this
* class extends.
* @param canvas the canvas on which to draw.
*/
public void repaint(GUI.Canvas canvas) {
// Draw a white rectangle with a black border.
canvas.setPenColor(Canvas.WHITE);
canvas.filledRectangle(x, y, width, height);
canvas.setPenColor(Canvas.BLACK);
canvas.setPenRadius(1.0);
canvas.rectangle(x+0.5, y+0.5, width-1, height-1);
// Draw a label in plain black font.
canvas.setFont(Canvas.DEFAULT_FONT);
canvas.setPenColor(Canvas.BLACK);
canvas.text(x + 75, y + 15, "Elapsed Time");
// Draw the number of mines in large red bold font.
// We cast to an integer so it doesn't show the decimal.
canvas.setFont(Canvas.BOLD_FONT);
canvas.setFont(24);
canvas.setPenColor(Canvas.DARK_RED);
int n = getElapsedSeconds();
String s = "" + n; // could also use String.format("%d", n)
canvas.text(x + 75, y + 45, s);
}
}