我相信 FPS一旦我启动这个程序,就会在我的新窗口中的某个地方打印。我看到该窗口,但其中未显示 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 将被打印到也在作者的开发环境中打开的某个控制台窗口中。 System.out.println
仍将打印到标准输出,除非您使用 setOut
更改它。并编写一些附加代码,然后将文本放入或绘制 Window
中 GUI 组件中的文本。
关于java - 为什么 FPS 不打印在我的窗口中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31640296/