-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.java
More file actions
70 lines (62 loc) · 2.77 KB
/
Copy pathTimer.java
File metadata and controls
70 lines (62 loc) · 2.77 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
/*
Name: Chelsea Wong & Annie Wong
Teacher: Ms. Basaraba
Date: January 18th, 2022
Description: Timer thread class used to create and display a timer object for the timed
game mode.
ISP: Crossword
*/
import java.awt.*; //gives access to java graphic related commands (ie. fonts and colors)
import hsa.Console; //gives access to Console class of the hsa library
import java.io.*; //gives access to the input output commands
class Timer extends Thread //Timer class
{
//declaring class variables
Console c;
int mins, secs;
boolean stop;
// imports the font files
public void loadFonts() throws java.awt.FontFormatException, java.io.IOException {
Font.createFont(Font.TRUETYPE_FONT, (InputStream) new FileInputStream("Fonts\\Montserrat-Regular.ttf"));
Font.createFont(Font.TRUETYPE_FONT, (InputStream) new FileInputStream("Fonts\\Montserrat-Bold.ttf"));
Font.createFont(Font.TRUETYPE_FONT, (InputStream) new FileInputStream("Fonts\\Montserrat-Italic.ttf"));
Font.createFont(Font.TRUETYPE_FONT, (InputStream) new FileInputStream("Fonts\\Montserrat-BoldItalic.ttf"));
}
// declaration of font constants [variables are in full uppercase since they are constants are do not change throughout the program]
final Font BODY = new Font("Montserrat Regular", Font.PLAIN, 20);
// declaration of color constants [variables are in full uppercase since they are constants are do not change throughout the program]
final Color LPURPLE = new Color(232,231,252);
final Color DPURPLE = new Color(87,88,208);
public Timer(Console con) throws java.awt.FontFormatException, java.io.IOException { //class constructor
mins = 0;
secs = 0;
stop = false;
c = con;
loadFonts();
}
public void run(){ //run method
String secsStr, minsStr;
while(!stop){ //while the timer isn't stopped
//converting seconds to minutes
if(secs == 59){
mins++;
secs = 0;
}
else{
secs++;
}
//displaying the timer
c.setColor(DPURPLE);
c.setFont(BODY);
secsStr = (secs < 10) ? "0"+secs : ""+secs; // if seconds is less than 10, add a 0 in front of it
minsStr = (mins < 10) ? "0"+mins : ""+secs; // if minutes is less than 10, add a 0 in front of it (not necessary but easier for formatting!)
c.drawString(minsStr + ":" + secsStr, 335, 91);
try{
Thread.sleep(1000); //1 second delay between incrementing
} catch(InterruptedException e){}
//erasing
c.setColor(LPURPLE);
c.fillRect(335, 74, 70, 24);
}
}
}