BufferedImage não sendo limpo antes de cada renderização
Estou tentando aprender a criar um jogo simples através de um tutorial que estou assistindo. Até agora, estava tudo bem, mas quando movo a imagem, a imagem anterior não é apagada ou descartada. Não sei exatamente o que está errado ou por que está acontecendo. Eu tenho 3 classes, uma classe principal, classe de jogador e uma classe bufferimageloader.
Classe principal:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Main extends Canvas implements Runnable {
private boolean running = false;
private Thread thread;
private BufferedImage player;
private Player p;
public void init(){ // load and initiliaze
BufferedImageLoader loader = new BufferedImageLoader();
try {
player = loader.loadImage("/player_shotgun2.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p = new Player(100, 100, this);
}
private synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
public void run() {
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;// 1 second divided by 60, run 60 times per second
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
System.out.println("hi");
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){// delta = 1 = 1 second
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer+= 1000;
System.out.println(updates + " Ticks, Fps " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
// Everything thats is updated in the game
private void tick(){
p.tick();
}
// Everything that is rendered in the game
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//////////////////////////////
p.render(g);
//////////////////////////////
g.dispose();
bs.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Main main = new Main();
JFrame window = new JFrame();
window.setSize(500,600);
window.setTitle("Zombie Game");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.add(main);
main.start();
}
public BufferedImage getPlayerImage(){
return player;
}
}
Classe de jogador:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class Player extends JPanel {
private double x;
private double y;
public BufferedImage player;
public Player(double x, double y, Main main){
this.x = x;
this.y = y;
player = main.getPlayerImage();
}
public void tick(){
x++;
}
public void render(Graphics g){
super.paintComponent(g);
g.drawImage(player, (int)x, (int)y, null);
g.dispose();
}
}
Classe Bufferedimageloader:
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path) throws IOException{
image = ImageIO.read(getClass().getResource(path));
return image;
}
}
Esta é a saída que recebo quando inicio e a imagem se move: