title: Java VSync without fullscreen window
body: <p>I'm trying to make my windowed application VSync with the monitor refresh. This code works properly with &quot;fullscreen = true&quot; (reports 75FPS, the monitors refresh rate) but in windowed mode it reports only 32FPS. Anybody know what I'm doing wrong here?</p>
  <pre><code>import java.awt.BufferCapabilities;
  import java.awt.GraphicsEnvironment;
  import java.awt.ImageCapabilities;
  import java.awt.image.BufferStrategy;
  import java.util.concurrent.TimeUnit;
  import javax.swing.JFrame;
  import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  
  public class VSyncTest {
  
      public static void main(String args[]) {
          final boolean fullscreen = true;
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  JFrame v = new JFrame();
                  if (fullscreen) {
                      GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(v);
                      v.createBufferStrategy(2);
                  } else {
                      v.setVisible(true);
                      ExtendedBufferCapabilities b = new ExtendedBufferCapabilities(new BufferCapabilities(new ImageCapabilities(true), new ImageCapabilities(true), BufferCapabilities.FlipContents.PRIOR), ExtendedBufferCapabilities.VSyncType.VSYNC_ON);
                      try {
                          v.createBufferStrategy(2, b);
                      } catch (Exception e) {
                          e.printStackTrace();
                          throw new RuntimeException(e);
                      }
                  }
                  new Thread() {
                      @Override
                      public void run() {
                          BufferStrategy bs = v.getBufferStrategy();
                          long start = System.nanoTime(), frames = 0;
                          do {
                              bs.getDrawGraphics();
                              bs.show();
                              frames++;
                              long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
                              long framesPerS = (long) (frames / (elapsed / 1000D));
                              if (frames % 60 == 0) {
                                  System.out.println(&quot;Frames/s: &quot; + framesPerS + &quot;, Elapsed: &quot; + elapsed / 1000);
                              }
  
                          } while (true);
                      }
                  }.start();
              }
          });
      }
  }
  </code></pre>