Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two things jump out at me.</p> <ol> <li>You "loop" is running without any kind of delay. This means that the screen will be updated as many times as it possible can, this may be to much for the hardware to keep up with. Generally speaking, you want to aim for around 25fps, this help provide the illusion of smooth movement</li> <li>You are not preparing the <code>Graphics</code> for painting. Each time you get a <code>Graphics</code> context from the <code>BufferStrategy</code> you are actually getting the last that was used. This means, everything that was painted to is still. There you need to clean this up. The flickering is (possibly) coming from the fact that one of the <code>Graphics</code> contexts has already being filled with a color, while the others have not.</li> </ol> <p>The following is a very basic example, including a little bit of animation</p> <pre><code>import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import javax.swing.JFrame; public class DoubleBuffer { public static void main(String[] args) { Pan game = new Pan(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); frame.add(game); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); game.start(); } public static class Pan extends Canvas implements Runnable { private int xDelta = 2; private int x = 0; private int y = 20; public Pan() { } public void start() { new Thread(this).start(); } public void run() { while (true) { update(); try { Thread.sleep(40); } catch (InterruptedException ex) { } draw(); } } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } public void update() { x += xDelta; if (x + 20 &gt; getWidth()) { x = getWidth() - 20; xDelta *= -1; } else if (x &lt; 0) { x = 0; xDelta *= -1; } } public void draw() { BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.RED); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.BLACK); g.fillOval(x, y, 20, 20); g.dispose(); bs.show(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload