To avoid flickering during the resize of my game window I'am using the sun.awt.noerasebackground property. This works oké for simple drawings. However when the drawing complexity increases so does the flickering! I have tried different things to get rid of the flickering but I'am running out of options.
By the way... I don't wan't to use Toolkit.getDefaultToolkit().setDynamicLayout(false). This eliminates flickering completely but makes it impossible to resize the drawing during a resize.
OS: Vista 64bit;
JRE: build 1.6.0_26-b03
Because code says more than a thousand words

see the example below. Use the static variable COMPLEXITY to increase the "complexity" of the drawing till the point the flickering starts.
Any suggestions to get rid of this flickering?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import java.awt.Canvas; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class FlickerDemo { private JFrame jFrame = null; private Canvas canvas = null; private BufferStrategy strategy = null; private static final int COMPLEXITY = 10000; public static void main(String[] args) { new FlickerDemo().start();
} public FlickerDemo() { System.setProperty("sun.awt.noerasebackground", "true"); GraphicsEnvironment graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice graphicsDev = graphicsEnv.getDefaultScreenDevice(); GraphicsConfiguration graphicsConf = graphicsDev.getDefaultConfiguration();
canvas = new Canvas(graphicsConf); canvas.setFocusTraversalKeysEnabled(false); canvas.setIgnoreRepaint(true); canvas.setBackground(Color.red); jFrame = new JFrame(graphicsConf); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setTitle("FlickerDemo"); jFrame.setFocusTraversalKeysEnabled(false); jFrame.setIgnoreRepaint(true); jFrame.setAlwaysOnTop(false); jFrame.setUndecorated(false); jFrame.add(canvas); jFrame.pack(); jFrame.setResizable(true); jFrame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); jFrame.setVisible(true); jFrame.setSize(800, 600); jFrame.setLocation(0, 0); canvas.createBufferStrategy(2); strategy = canvas.getBufferStrategy(); }
public void start() { while(true) { Dimension windowSize = canvas.getSize(); int screenX = windowSize.width; int screenY = windowSize.height; Graphics2D g = (Graphics2D)strategy.getDrawGraphics(); g.setColor(Color.black); g.fillRect(0, 0, screenX,screenY);
draw(g, screenX, screenY); g.dispose(); strategy.show(); } } private void draw(Graphics2D g, int screenX, int screenY) { g.setColor(Color.red); for(int i=0; i<COMPLEXITY; i++) { g.fillRect(screenX / 2 - 10, screenY / 2 - 10, 20, 20); } } } |