I'm in the process of trying to get familiar with making games in Java, using the Swing library.

Coding my Snake game however, I've got to a point where the game is flickering/ghosting and I haven't a clue why.

An example of the flickering can be seen on this short youtube video: http://www.youtube.com/watch?v=QP1IcZ5TRoY

I dread to post too much code here as it won't help anyone, but for glancers, I'll post some facts.

  • Painting on a JComponent
  • I do call super.paintComponent(g) at the start of my overrriden paintComponent method
  • I've set setDoubleBuffered(boolean) to true.
  • Although unsure of its necessity, I've invoked createBufferStrategy(2) on my JFrame
  • My "game loop" is based on a swing.Timer calling repaint every 17ms, but tried, 30fps
  • Snake piece moves at 25 pixel paces (or relative to world size).

This is how I draw each tile:

    for (int i = 0; i < gamePieces.length; i++) {
        int x = i % ROW_COL_SIZE;
        int y = i / ROW_COL_SIZE;

        if (gamePieces[i].equals(Tile.EMPTY)) {

        } else {
            final SnakeGraphics gfx = gamePieces[i].getGraphic();
            gfx.draw(g2d, x, y, direction); 

This is what is called on repaint:

public void render(Graphics2D g2d) {
    Direction dir = keyboard.getDirection();
    gameWorld.draw(g2d, dir);


and finally, how the snake moves:

public boolean move() {
    Point snakeHead = snake.getFirst();
    int x = snakeHead.x;
    int y = snakeHead.y;

    boolean success = validateStep(snakeHead); //boundary control + move

    if (success) {
        gameWorld.setTile(x, y, Tile.EMPTY);
        gameWorld.setTile(snakeHead.x, snakeHead.y, Tile.SNAKE_HEAD);
    return success;


where gameWorld has all the tile information, i.e. what tiles get drawn and where, and Snake is just current position of the head thus far.

Apologies for the code-drop, but its all that I can determine to be relevant.

  • \$\begingroup\$ My honest advice is stop using Java. XNA is free, well document, has tutorials, and to top it off C# has a Java-like syntax. There is no excuse to use a toolkit like Swing for making a game when XNA is freely available. The same amount of time you invest working around Swing's handicaps wrt games can be much better invested in working on a game through an API that isn't working against you. \$\endgroup\$
    – bobobobo
    Aug 12, 2012 at 19:15
  • \$\begingroup\$ I appreciate your advice being sensible ;p but as I enjoy coding in Java I just decided to get to know its Swing libraries a bit better by trying out some basic games. I obviously overestimated its ease of use how such tasks however, as "passive" rendering games in the past used to not give near as much issues :D. \$\endgroup\$
    – Blueski
    Aug 12, 2012 at 19:20

1 Answer 1


I'm not experienced enough to say why exactly your game is flickering but I suspect it is because you use Swing. Swing is a good tool to develop 'regular' software but not for creating games / graphical software.

I did some research on this a while ago. Sadly I haven't taken good care of my documentation and lost some valuable links on the subject. However what you are looking for is not event-based rendering (like swing) but active rendering.

You should do your own research and determine what makes sense to you. Here are two links that may get you started: java games active rendering and Double Buffering and Active Rendering in Java with Swing Integration.

This is how I do it in my code:

At inistialisation:

        // Create game screen
        window = new JFrame("GameTitle"); // Create a new JFrame window
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set close operation
        window.setIgnoreRepaint(true); // Make active rendering possible
        window.setResizable(false); // Disable window resize

        // Create GUI, add to the screen
        Canvas screen = new GameScreen(); // Create canvas
        screen.setPreferredSize(new Dimension(800, 800)); // Set size
        window.add(screen); // Add to the window
        window.pack(); // Pack the window to fit the canvas set size
        window.setVisible(true); // Set visible

        // Create BufferStrategy object
        screen.createBufferStrategy(2); // Double buffer

Rendering inside the game loop:

        // Get buffer            
        BufferStrategy buffer = screen.getBufferStrategy();

        // Get context from the JPanel we are going to draw to
        Graphics g = buffer.getDrawGraphics();

        // Start with clearing the drawing surface
        g.fillRect(0, 0, screen.getWidth(), screen.getHeight());

        // Call render processes (NOTE: this is my own render architecture)
        renderImage.run(entityManager, entityManager.getGroup("RENDERABLE"));

        // After we draw everything to the back buffer, we show that buffer
  • \$\begingroup\$ Thanks user8363. The material you gave me was a very good read, and quite scary to be honest, at least in retrospect to passive rendering code. Out of curiosity however, you provide such personal code segments but admit to not being experienced with Swing, so what libraries do you use for any projects you undertake. Thanks. \$\endgroup\$
    – Blueski
    Aug 12, 2012 at 19:21
  • \$\begingroup\$ Just everything the JDK has to offer without making much use of the Swing libraries but more of AWT. \$\endgroup\$
    – user8363
    Aug 12, 2012 at 20:37
  • \$\begingroup\$ Oh btw I wouldn't think of active rendering as particularly scary. You just have more control. I think understanding active rendering together with game loops will get you a very long way in understanding the architecture of a game. \$\endgroup\$
    – user8363
    Aug 12, 2012 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .