【发布时间】:2016-01-24 21:00:29
【问题描述】:
我正在尝试通过我正在观看的教程来学习如何构建一个简单的游戏。到目前为止一切都很好,但是当我移动图像时,前一个图像不会被删除或处置。我不确定到底出了什么问题,或者为什么会这样。我有 3 个类,一个主类、一个播放器类和一个 bufferimageloader 类。
主类:
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;
}
}
播放器类:
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();
}
}
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;
}
}
这是我启动并且图像移动时得到的输出:
【问题讨论】:
标签: java bufferedimage