eclipse在java中使用映像创建类实例
我一直在youtube上关注这个用java创建游戏的教程,在教程的某一点上,你应该加载一个64x64 PNG图像,读取每个像素的RGB值,并相应地创建类。我没有收到任何错误,但我得到的只是一个空白屏幕。当像素为蓝色时,程序应该创建播放器类的实例;当像素为红色时,程序应该创建块类的实例
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
private BufferedImage level = null;
public Game(){
new Window(1000, 563, "Wizard Game", this);
start();
handler = new Handler();
this.addKeyListener(new KeyInput(handler));
BufferedImageLoader loader = new BufferedImageLoader();
level = loader.LoadImage("wizard_level.png");
loadLevel(level);
}
private void start(){
isRunning = true;
thread = new Thread(this);
thread.start();
}
private void stop(){
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 60;
while(isRunning){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
delta --;
}
if(isRunning)
render();
frames ++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
public void tick(){
handler.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 1000, 563);
handler.render(g);
g.dispose();
bs.show();
}
//loading level
private void loadLevel(BufferedImage image){
int w = image.getWidth();
int h = image.getHeight();
for(int xx = 0; xx > w; xx++){
for(int yy = 0; yy < h; yy++){
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int blue = (pixel >> 8) & 0xff;
int green = (pixel) & 0xff;
if(red == 255)
handler.addObject(new Block(xx*32, yy*32, ID.Block));
if(blue == 255)
handler.addObject(new Wizard(xx*32, yy*32, ID.Player, handler));
}
}
}
public static void main(String args[]){
new Game();
}
}
# 1 楼答案
在
level = loader.LoadImage("wizard_level.png");
行上,假设为level = loader.LoadImage("/wizard_level.png");
您忘记了路径开头的/
,否则编译器将把它作为字符串读取