BufferStrategy - it doesn't update graphics

Hi, I’m having problems with BufferStrategy… for some reason I can’t manage to update the graphics in it:

public void Draw()
    {
        backbuffer = this.getBufferStrategy();
        
        do 
        {
            try{
                    graphics = backbuffer.getDrawGraphics();
                    DrawAll(); //this draws thing on "graphics"
            }
            finally
            {
                graphics.dispose();
            }

            backbuffer.show();
            Toolkit.getDefaultToolkit().sync();
        } while (backbuffer.contentsLost());
    }

It is necessary to say that I always call this method? but for some reason I can’t see my … new graphics?
If you need more details, just tell me.

Thanks.

We’re probably gonna need to see more code to provide much useful help, for instance the DrawAll() method, and how/when things are initiated would be useful.

First, although I don’t think it should make a difference (could be wrong, it’s been awhile), I would dispose the graphics after backbuffer.show(), as per http://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html

the only way I actually managed to “correctly” update the drawing, is by drawing something over it, using:

graphics.setColor (getBackground ());
graphics.fillRect (0, 0, this.getSize().width, this.getSize().height);

but, isn’t

graphics.dispose();

supposed to clear the graphics and let space for the incoming?

thanks

No. Graphics.dispose() basically destroys that particular graphics context, and any calls to it after are ignored.

Also, instead of setting the current color to the background color and fillRect, you can simply: clearRect()
That uses the background color (hence ‘clear’ as in wipe off) automatically.

[quote]First, although I don’t think it should make a difference (could be wrong, it’s been awhile), I would dispose the graphics after backbuffer.show(), as per http://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html
[/quote]
this link dispose the graphics before showing them from backbuffer

good tip

Quick working example usage of BufferStrategy:


import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class BSTest {
	
	public static void main(String[] a) throws InterruptedException {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		
		frame.createBufferStrategy(2);
		
		while (true) {
			Graphics g = null;
			t: try {
				g = frame.getBufferStrategy().getDrawGraphics();
				if (g == null) break t;
				render(g);
			} finally {
				g.dispose();
			}
			frame.getBufferStrategy().show();
			Thread.sleep(20);
		}
	}
	
	private static void render(Graphics g) {
		g.clearRect(0, 0, 400, 400);
		g.setColor(Color.blue);
		g.fillOval((int) System.currentTimeMillis() % 400, (int) System.currentTimeMillis() % 400, 10, 10);
	}
}

This works fine for me, take from it what you will.

Thanks, looks pretty similar to my approach, but I think that what does the trick is

    g.clearRect(0, 0, 400, 400);
      g.setColor(Color.blue);

so, Solved!