为什么 FPS 不打印在我的 window 中?
Why doesn't the FPS print in my window?
我相信一旦我启动这个程序,FPS 就会打印在我的新 window 中的某个地方。我看到 window 但 FPS 没有显示在其中。怎么了?
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public Game() {
new Window(WIDTH, HEIGHT, "MY GAME", this);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta <= 1) {
delta--;
}
if (running)
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("fps: " + frames);
frames = 0;
}
}
stop();
}
public static void main(String args[]) {
new Game();
}
}
该教程可能意味着 fps 将打印到某个控制台 window,该控制台也在作者的开发环境中打开。 System.out.println
仍将打印为标准输出,除非您将其更改为 setOut
并编写一些额外的代码,然后在 Window
.[=14 的某处的 GUI 组件中放置或绘制文本=]
我相信一旦我启动这个程序,FPS 就会打印在我的新 window 中的某个地方。我看到 window 但 FPS 没有显示在其中。怎么了?
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public Game() {
new Window(WIDTH, HEIGHT, "MY GAME", this);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta <= 1) {
delta--;
}
if (running)
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("fps: " + frames);
frames = 0;
}
}
stop();
}
public static void main(String args[]) {
new Game();
}
}
该教程可能意味着 fps 将打印到某个控制台 window,该控制台也在作者的开发环境中打开。 System.out.println
仍将打印为标准输出,除非您将其更改为 setOut
并编写一些额外的代码,然后在 Window
.[=14 的某处的 GUI 组件中放置或绘制文本=]