-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerThread.java
More file actions
137 lines (114 loc) · 6.17 KB
/
TimerThread.java
File metadata and controls
137 lines (114 loc) · 6.17 KB
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
127
128
129
130
131
132
133
134
135
/* Justin Pu
Spinnin' Tiles
December 30, 2014
Ms. Dyke
Description:
This class implements a timer that counts how long the user has been playing the game for.
Variable Dictionary
_______________________________________________________________________________________________________________________
| Type | Name | Description |
|-----------|-----------|-----------------------------------------------------------------------------------------------|
| Console | c | The reference to the output Console. Has a text size of 30 by 100. Also allows access to |
| | | the built-in methods. |
|-----------|-----------|-----------------------------------------------------------------------------------------------|
| int | time | The total amount of time in seconds that the user has been playing the game for. | |
|-----------|-----------|-----------------------------------------------------------------------------------------------|
| boolean | count | Holds whether to continue counting or not in case the game has ended. |
|-----------|-----------|-----------------------------------------------------------------------------------------------|
| Color | bg | Holds the background colour. |
|-----------|-----------|-----------------------------------------------------------------------------------------------|
| Color | fg | Holds the foreground colour. |
|___________|___________|_______________________________________________________________________________________________|
*/
import java.lang.*;
import java.awt.*;
import hsa.Console;
public class TimerThread extends Thread
{
private Console c;
private int time;
private boolean count;
private Color bg;
private Color fg;
// This method draws the timer on the top bar of the game window.
public void draw ()
{
int seconds = time % 60;
int minutes = (time / 60) % 60;
int hours = time / 60 / 60;
String temp = ((hours / 10 > 0) ? ( "" + hours) : ("0" + hours)) + ":" + ((minutes / 10 > 0)? ("" +minutes) : ("0" + minutes)) + ":" + ((seconds / 10 > 0) ? ("" + seconds) : ("0" + seconds));
c.setTextBackgroundColour (fg);
c.setTextColour (bg);
c.setCursor (1, 1);
c.print (' ', 50 - temp.length () / 2);
c.println (temp);
c.setTextBackgroundColour (bg);
c.setTextColour (fg);
}
/* This method runs the Thread and counts the time.
The try block delays the Thread for one second.
An InterruptedException is caught in case it can't be slept.
The while loop continues as long as the Thread is instructed to count up (count is true).
The try block in the Thread delays the program so that one second can pass before a second is added to the time.
An InterruptedException is caught in case the program can't be delayed.
Local Variable Declaration
___________________________________________________________________________________________________________________________________
| Type | Name | Description |
|-----------------------|---------------|-------------------------------------------------------------------------------------------|
| InterruptedException | e | Catches an InterruptedException in case the program can't be delayed properly. |
|-----------------------|---------------|-------------------------------------------------------------------------------------------|
| InterruptedException | e | Catches an InterruptedException in case the program can't be delayed properly. |
|_______________________|_______________|___________________________________________________________________________________________|
*/
public void run ()
{
try
{
sleep (1000);
}
catch (InterruptedException e)
{
}
count = true;
while (count)
{
time++;
draw ();
try
{
sleep (1000);
}
catch (InterruptedException e)
{
}
}
}
// Stops the counter from running.
public void stopCounter ()
{
count = false;
}
// Returns the amount of seconds that the user has been playing the game for.
public int getSeconds ()
{
return time;
}
/* The constructor creates the instance of the program. It also sets the Console window and the background and forground colours.
Local Variable Declaration
___________________________________________________________________________________________________________________________________
| Type | Name | Description |
|-----------------------|---------------|-------------------------------------------------------------------------------------------|
| Console | Con | Gives the Thread the same Console window to draw the time. |
|-----------------------|---------------|-------------------------------------------------------------------------------------------|
| Color | BG | Passes the same background colour as in the other classes. |
|-----------------------|---------------|-------------------------------------------------------------------------------------------|
| Color | FG | Passes the same forground colour as in the other classes. |
|_______________________|_______________|___________________________________________________________________________________________|
*/
public TimerThread (Console con, Color BG, Color FG)
{
c = con;
bg = BG;
fg = FG;
}
}