title: FPS Counter in my game (Java)
body: <p>I need to add a working FPS counter in my game (Pong). To make an FPS counter I apparently also need a game loop, I tried making a game loop and FPS counter separately and it seems to be working. However, I am having some problems adding the FPS counter to my Pong game.</p>
  
  <p>This is the code for the game loop/FPS counter:</p>
  
  <pre><code>import java.awt.Canvas;
  import java.awt.Dimension;
  import java.awt.Image;
  import java.awt.Point;
  import java.util.Random;
  
  import javax.swing.JFrame;
  import javax.swing.Timer;
  
  public class Main extends Canvas implements Runnable{
  
  JFrame frame;
  int windowWidth, windowHeight;
  Point bollXY;
  int screenWidth, screenHeight;
  Timer timer;
  Image bildPaddel;
  Image bildBollen;
  int paddelY;
  int paddel2Y;
  boolean paddelUp, paddelDown;
  Random rand;
  int score, score2;
  boolean bollUp, bollRight, changeDirection;
  boolean paused;
  int fruktDistansRand;
  long time;
  int fps, newfps;
  
  private boolean running = false;
  private Thread thread;
  
  
  
  private synchronized void start() {
      if(running)
          return;
  
      running = true;
      thread = new Thread(this);
      thread.start();
  }
  
  private synchronized void stop(){
      if(!running)
          return;
  
      running = false;
      try {
          thread.join();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      System.exit(1);
  }
  
  public static void main(String args[]) {
  
      Main main = new Main();
  
      JFrame frame = new JFrame();
      frame.add(main);
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  
      main.start();
  }
  
  
  @Override
  public void run() {
      long lastTime = System.nanoTime();
      final double amountOfTicks = 60.0;
      double ns = 1000000000 / amountOfTicks ;
      double delta = 0;
      int updates = 0;
      int frames = 0;
      long timer = System.currentTimeMillis();
  
      while(running) {
          long now = System.nanoTime();
          delta += (lastTime - now) / ns;
          lastTime = now;
          if(delta &gt;= 1) {
              updates++;
              delta--;
          }
          frames++;
  
          if(System.currentTimeMillis() - timer &gt; 1000) {
              timer += 1000;
              System.out.println("Ticks: " + updates + ", FPS: " + frames);
              updates = 0;
              frames = 0;
          }
      }
      stop();
  }
  }
  </code></pre>
  
  <p>I tried adding the FPS counter to the game but it did not work for some reason. If you know how to make a better FPS counter and/or how to add it to the game please help. By the way, I do NOT want the FPS locked at 60.</p>