title: Smooth Drawing using Java2d without the Opengl or Direct3d Pipelines?
body: <p>I can't figure out a way to get smooth movement or animation of anything using Java2d when the opengl and direct3d pipelines are disabled (by invoking the vm with -Dsun.java2d.d3d=false and -Dsun.java2d.opengl=false)</p>
  
  <p>The quick and dirty code below demonstrates my problem. It draws a box that moves across the screen. The box location is updated about 60 times per second and the screen is redrawn as many times as possible. It uses the BufferStrategy class to implement double buffering; the flip is done at "bs.show();"</p>
  
  <p>Code(press escape to quit): </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Frame;
  import java.awt.Graphics;
  import java.awt.GraphicsConfiguration;
  import java.awt.GraphicsDevice;
  import java.awt.GraphicsEnvironment;
  import java.awt.Rectangle;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.image.BufferStrategy;
  
  public class FluidMovement {
      private static volatile boolean running = true;
      private static final int WIDTH = 500;
      private static final int HEIGHT = 350;
  
      public static void main(String[] args) {
          GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
          GraphicsDevice gd = ge.getDefaultScreenDevice();
          GraphicsConfiguration gc = gd.getDefaultConfiguration();
  
          Frame frame = new Frame(gc);
          frame.setIgnoreRepaint(true);
          frame.setUndecorated(true);
          frame.addKeyListener(new KeyAdapter() {
              @Override public void keyPressed(KeyEvent e) {
                  if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                      running = false;
                  }
              }
          });
          frame.setSize(WIDTH, HEIGHT);
          frame.setVisible(true);
          frame.createBufferStrategy(2);
          BufferStrategy bs = frame.getBufferStrategy();
          long nextTick = System.nanoTime();
          class Rect {
              int dx = 2, dy = 1, x = 0, y = 0;
          }
          Rect rect = new Rect();
  
          Graphics g;
          while (running) {
  
              if (System.nanoTime() &gt; nextTick) {
                  rect.x = (rect.x + rect.dx) % WIDTH;
                  rect.y = (rect.y + rect.dy) % HEIGHT;
                  nextTick += 1000000000 / 60;
              }
  
              g = bs.getDrawGraphics();
              g.setColor(Color.BLACK);
              g.fillRect(0, 0, WIDTH, HEIGHT);
              g.setColor(Color.WHITE);
              g.fillRect(rect.x, rect.y, 10, 10);
              g.dispose();
              bs.show();
          }
          bs.dispose();
          frame.dispose();
      }
  
  }
  </code></pre>
  
  <p>When I execute this code normally with "java FluidMovement", it runs smooth as silk (besides the occasional tearing) because the jvm makes use of the direct3d/directdraw pipeline. When i execute this code with "java -Dsun.java2d.d3d=false -Dsun.java2d.opengl=false FluidMovement" it is terribly choppy. </p>
  
  <p>I can't make the assumption that the direct3d or opengl pipeline is used. The pipelines don't work with 2 of the 3 machines I have tried it on; it only worked on a machine with dedicated graphics running Windows 7. Is there anyway I can make the box move smoothly or should i resort to using some kind of library with low level access like JOGL?</p>
  
  <p>Notes:</p>
  
  <ul>
  <li>Frame rate is not the issue. In both cases (pipelines enabled and disabled), the application runs well over 300 fps. I forced vsync off when the pipelines are enabled. </li>
  <li>I've tried Toolkit.getDefaultToolkit().sync()</li>
  <li>I've tried many different types of loops, but the movement is never truly smooth. Even with a fixed framerate the same choppiness is exhibited.</li>
  <li>I've tried running the frame in full-screen exclusive mode.</li>
  <li>I've tried using 3 or even 4 buffers.</li>
  </ul>