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.