package game;

import javax.swing.JFrame;

public class Game extends JFrame implements Runnable {
  private Thread thread;
  private Screen screen;
  private boolean running = false;

  public Game() {
    super("Game Loop");

    screen = new Screen();
    add(screen);

    pack();
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }

  @Override
  public void run() {

  }

  public synchronized void start() {
    if (running) return;
    running = true;
    thread = new Thread(this);
    thread.start();
  }

  public synchronized void stop() {
    if (!running) return;
    running = false;
    try {
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

@@ -175,16 +175,47 @@
 = false;
+%0A  private final int UPS = 120;
 %0A%0A  publ
@@ -424,48 +424,8 @@
  %7D%0A%0A
-  @Override%0A  public void run() %7B%0A%0A  %7D%0A%0A
   pu
@@ -707,24 +707,24 @@
 eption e) %7B%0A
-
       e.prin
@@ -747,11 +747,974 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  public void run() %7B%0A    double secondsPerTick = 1.0 / UPS;%0A%0A    double nextTime = System.nanoTime() / 1000000000.0;%0A    double maxTimeDiff = 0.5;%0A    int skippedFrames = 1;%0A    int maxSkippedFrames = 5;%0A%0A    while (running) %7B%0A      double curTime = System.nanoTime() / 1000000000.0;%0A      if ((curTime - nextTime) %3E maxTimeDiff) nextTime = curTime;%0A      if (curTime %3E= nextTime) %7B%0A        // do update code, this will get run UPS times a second%0A        nextTime += secondsPerTick;%0A        if ((curTime %3C nextTime) %7C%7C (skippedFrames %3E maxSkippedFrames)) %7B%0A          // do rendering code%0A          skippedFrames = 1;%0A        %7D else %7B%0A          ++skippedFrames;%0A        %7D%0A      %7D else %7B%0A        int sleepTime = (int) (1000.0 * (nextTime - curTime));%0A        if (sleepTime %3E 0) %7B%0A          try %7B%0A            Thread.sleep(sleepTime);%0A          %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A          %7D%0A        %7D%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

write main game loop