-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameRate.java
39 lines (33 loc) · 1.24 KB
/
FrameRate.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
import java.awt.Color;
import java.awt.Graphics;
class FrameRate {
String frameRate; // to display the frame rate to the screen
long lastTimeCheck; // store the time of the last time the time was recorded
long deltaTime = 0; // to keep the elapsed time between current time and
// last
// time
int frameCount; // used to count how many frame occurred in the elasped time
// (fps)
public FrameRate() {
lastTimeCheck = System.currentTimeMillis();
frameCount = 0;
frameRate = "0 fps";
}
public void update() {
long currentTime = System.currentTimeMillis(); // get the current time
deltaTime += currentTime - lastTimeCheck; // add to the elapsed time
lastTimeCheck = currentTime; // update the last time var
frameCount++; // everytime this method is called it is a new frame
if (deltaTime >= 1000) { // when a second has passed, update the string
// message
frameRate = frameCount + " fps";
frameCount = 0; // reset the number of frames since last update
deltaTime = 0; // reset the elapsed time
}
}
public void draw(Graphics g, int x, int y) {
//System.out.println(frameRate);
g.setColor(Color.WHITE);
g.drawString(frameRate, x, y); // display the frameRate
}
}