help with double buffer

I am starting to work with double buffering with BufferedStrategy and my screen doesn’t clear. well here is my code.

StarHawk.class ( the main class)

@Override
	public void init() {
	}

	@Override
	public void update(long delta, Window w) {
		w.addMouseMotionListener(new MouseMotionListener(){
			@Override
			public void mouseDragged(MouseEvent paramMouseEvent) {}

			@Override
			public void mouseMoved(MouseEvent e) {
				x = e.getX();
				y = e.getY();
				
			}
			
		});
		
	}
	
	@Override
	public void render(Graphics2D g, Window w) {
		g.fillRect(x, y, 200, 200);
		
	}
	
	public static void main(String[] args) {
		GameContainer game = new GameContainer(new StarHawk());
		game.setScreenMode(1280, 1024, true);
		game.setTitle("Mini StarHawk Game");
		game.EscapeOnClose();
		game.startGame();
		
	}

GameContainer.class

public void startGame() {
		System.out.println(engineName+"Game Started"+getDate());
		setupListenerFrame();
		if(isFullscreen) {
			setFullScreen(dm);
		}
		frame.createBufferStrategy(2);         // frame is a JFrame
		
		new Thread(){
			public void run() {
				game.init();               // abstract method in StarHawk class
				isRunning = true;
				long lastLoopTime = System.currentTimeMillis();
				while(isRunning) {
					long delta = System.currentTimeMillis() - lastLoopTime;
					lastLoopTime = System.currentTimeMillis();
					Graphics2D g = getGraphics(); // gets graphics2D 
					game.update(delta,getFullScreenWindow()); // abstract method in StarHawk class
					game.render(g,getFullScreenWindow());     // abstract method in StarHawk class
					g.dispose();
					update(); // update method
					
					try {Thread.sleep(10);} catch (InterruptedException e) {}
				}
				
			}
			
		}.start();
	}

update method from GameContainer.class

public void update() {
		Window window  = vc.getFullScreenWindow();
		if(window != null) {
			strategy = window.getBufferStrategy();
			if(!strategy.contentsLost()) {
				strategy.show();
			}
		}

getGraphics method from GameContainer.class

public Graphics2D getGraphics() {
        Window window = vc.getFullScreenWindow();
        if (window != null) {
            BufferStrategy strategy = window.getBufferStrategy();
            return (Graphics2D)strategy.getDrawGraphics();
        }
        else {
            return null;
        }
    }

i don’t know what i did wrong. the screen doesn’t flicker but it doesn’t clear it.

you should clear it by yourself, BufferStrategy does not do it automatically .


    @Override
    public void render(Graphics2D g, Window w) {
        g.setColor (clearColor);
        g.fillRect(0,0,screenWidth, screenHeight);
        g.setColor ( rectColor);

         // now start drawing stuff
        g.fillRect(x, y, 200, 200);
         
    }


Thank you :slight_smile:
I thought the buffer Strategy would do that but i was wrong.
instead of doing


    g.setColor (clearColor);
    g.fillRect(0,0,screenWidth, screenHeight);
    g.setColor ( rectColor);

i did

g.clearRect(0, 0, w.getWidth(), w.getHeight());

found that, that method worked instead

clearRect is shorter, but a lot of time, ur going to want to have another background than white. So if you have white that works great, but just keep in mind teletubo’s way so that later on you’ll be set

You can use g.setBackground(Color c); to change which color clearRect uses.

touché :wink:

thanks for the those tips. :slight_smile: