簡體   English   中英

為什么FPS不在我的窗口中打印?

[英]Why doesn't the FPS print in my window?

我相信,一旦啟動此程序, 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對其進行setOut並編寫一些其他代碼,然后將其放置或繪制在Window的GUI組件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM