jar文件中的图像不会显示

时间:2013-12-21 21:16:34

标签: java eclipse jar embedded-resource

所以我的麻烦是,当我将这个游戏放入.JAR文件时,它不会出现。我仍然可以看到子弹,但我看不到入侵者的图像

public class Invaders {
protected int xPos;
protected int yPos; 
protected int moveStep = 0;  
protected int size = 15; 


static BufferedImage invader1image = DrawingPanel.loadBitmap("invader1.jpg");
static BufferedImage invader2image = DrawingPanel.loadBitmap("invader2.jpg");

//some code in the middle omitted


public void draw(Graphics g, int seconds) {

    if(seconds%50 < 25){
    g.drawImage(invader1image, 
            this.xPos-this.size, this.yPos-this.size,
            this.size*2, this.size*2,null);
    } else{ g.drawImage(invader2image, // this isn't working
            this.xPos-this.size, this.yPos-this.size,
            this.size*2, this.size*2,null);

    }
}

1 个答案:

答案 0 :(得分:3)

你应该按照以下方式获取图像

BufferedImage invader1image = ImageIO.read(getClass().getResource("invader1.jpg"));

现在您将图像保存在类文件中