title: How do I make the frame rate consistent?
body: <p>I'm making simple 2D games. I have a game loop, and in the game loop i have an update
  method. I make things move by adding 1 to xPos whenever it loops. This means that if you have a slow fps then everything goes in slow motion and if you have a high fps everything moves
  really quick.</p>
  
  <p>This is my code:</p>
  
  <pre><code>    long fpsTimer;
      int frames;
      public void run(){
          running = true;
          fpsTimer = System.nanoTime();
  
          while(running){
              render();
              update();
  
              try{
                  Thread.sleep(6);
              }catch(InterruptedException e){}
              frames++;
  
              if(System.nanoTime() &gt;= fpsTimer+1000000000){
                  System.out.println(frames+" fps");
                  frames = 0;
                  fpsTimer = System.nanoTime();
              }
          }
      }
  </code></pre>
  
  <p>All Code</p>
  
  <pre><code>    import java.awt.*;
      import java.awt.image.*;
      import javax.swing.JFrame;
      import java.awt.event.*;
  
      public class Game extends Canvas implements Runnable{
          public static final int WIDTH = 800;
          public static final int HEIGHT = 300;
          public JFrame f;
          private String title = "Untitled Test";
          private Image image;
          private Sprite player;
  
          public Game(){
              player = new Sprite(100, 100);
              setPreferredSize(new Dimension(WIDTH, HEIGHT));
              setMaximumSize(new Dimension(WIDTH, HEIGHT));
              setMinimumSize(new Dimension(WIDTH, HEIGHT));
  
              addKeyListener(new KeyAdapter(){
                  public void keyPressed(KeyEvent e){
                      player.keyPressed(e.getKeyCode());
                  }
  
                  public void keyReleased(KeyEvent e){
                      player.keyReleased(e.getKeyCode());
                  }
              });
          }
  
          public static void main(String[] args){
              Game g = new Game();
              g.f = new JFrame(g.title);
              g.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              g.f.add(g);
              g.f.setResizable(false);
              g.f.pack();
              g.f.setLocationRelativeTo(null);
              Thread gameLoop = new Thread(g);
              gameLoop.start();
              g.f.setVisible(true);
          }
  
          private void render(){
              BufferStrategy bs = getBufferStrategy();
              if(bs==null){
                  createBufferStrategy(3);
                  return;
              }
  
              image = createImage(WIDTH, HEIGHT);
              Graphics g = image.getGraphics();
  
              player.draw(g);
  
              g.dispose();
              Graphics bsg = bs.getDrawGraphics();
              bsg.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
              bsg.dispose();
              bs.show();
          }
  
          private void update(){
              player.move();
          }
  
          long fpsTime;
          int frames;
  
          public void run(){
              fpsTime = System.nanoTime();
  
              while(true){
  
                  render();
                  update();
  
                  try{
                      Thread.sleep(6);
                  }catch(InterruptedException e){}
  
                  frames++;
                  if(System.nanoTime() &gt;= fpsTime+1000000000){
                      System.out.println(frames+" fps");
                      frames = 0;
                      fpsTime = System.nanoTime();
                  }
              }
          }
      }
  </code></pre>