-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameTimer.java
More file actions
99 lines (82 loc) · 3.49 KB
/
Copy pathGameTimer.java
File metadata and controls
99 lines (82 loc) · 3.49 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class draws a timer on the top right of the player screen
*/
import java.awt.*;
import java.text.DecimalFormat;
public class GameTimer {
// Constants for positioning the timer on the screen
public static final int TOP_MARGIN = 50; // Distance from the top of the screen
public static final int RIGHT_MARGIN = 20; // Distance from the right edge of the screen
public long startTimeMillis; // Stores the start time in milliseconds
public boolean running; // Indicates whether the timer is running
public final Font TIMER_FONT; // Font used to display the timer
public final Color TEXT_COLOR = Color.WHITE; // Color of the timer text
public final Color SHADOW_COLOR = new Color(0, 0, 0, 150); // Semi-transparent shadow color for text
public final DecimalFormat TIME_FORMAT = new DecimalFormat("00"); // Format for displaying time in two digits
// Constructor to initialize the timer with a specific font
public GameTimer(Font font) {
this.TIMER_FONT = font.deriveFont(24f); // Set font size to 24
this.running = false; // Timer starts in a stopped state
}
// Starts the timer by recording the current time
public void start() {
startTimeMillis = System.currentTimeMillis();
running = true;
}
// Stops the timer
public void stop() {
running = false;
}
// Resets the timer to the current time
public void reset() {
startTimeMillis = System.currentTimeMillis();
}
// Calculates the elapsed time in milliseconds since the timer started
public long getElapsedTimeMillis() {
if (!running) {
return 0; // If the timer is not running, return 0
}
return System.currentTimeMillis() - startTimeMillis;
}
// Draws the timer on the screen
public void draw(Graphics2D g2d, int screenWidth) {
long elapsedMillis = 0L;
long seconds = 0L;
long minutes = 0L;
long hours = 0L;
String timeString = null;
FontMetrics fm = null;
int textWidth = 0;
int textX = 0;
int textY = 0;
if (!running) {
return; // Do nothing if the timer is not running
}
// Calculate elapsed time in hours, minutes, and seconds
elapsedMillis = getElapsedTimeMillis();
seconds = (elapsedMillis / 1000) % 60;
minutes = (elapsedMillis / (1000 * 60)) % 60;
hours = elapsedMillis / (1000 * 60 * 60);
// Format the time string based on whether hours are present
if (hours > 0) {
timeString = hours + ":" + TIME_FORMAT.format(minutes) + ":" + TIME_FORMAT.format(seconds);
} else {
timeString = minutes + ":" + TIME_FORMAT.format(seconds);
}
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setFont(TIMER_FONT); // Set the font for the timer text
// Calculate the position of the timer text (top-right corner)
fm = g2d.getFontMetrics();
textWidth = fm.stringWidth(timeString);
textX = screenWidth - textWidth - RIGHT_MARGIN;
textY = TOP_MARGIN + fm.getAscent();
// Draw a shadow for the text to improve visibility
g2d.setColor(SHADOW_COLOR);
g2d.drawString(timeString, textX + 2, textY + 2);
// Draw the timer text
g2d.setColor(TEXT_COLOR);
g2d.drawString(timeString, textX, textY);
}
}