我无法在JAR文件中加载Image文件

时间:2013-03-07 12:31:44

标签: java embedded-resource bufferedimage

这可能有重复,但我似乎无法找到一个解决我的问题。

我的问题是我的3个图像文件中有1个不会从已编译的JAR文件中加载。我可以加载它的唯一方法是图像是在.jar文件之外。

我的3个图像文件名:

Tiles.png <---One that only loads outside
Sheet.png
Level.png

SpriteSheet.java&lt; ----抓住精灵并给出图块大小

public class SpriteSheet {
private BufferedImage image = null;
private BufferedImage[][] tiles = null;

public int tileSize = 0;

public SpriteSheet(String file, int tileSize) {
    this.tileSize = tileSize;
    try {
        InputStream url = SpriteSheet.class.getResourceAsStream(file);
        image = ImageIO.read(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    split();
}

Level.java&lt; ----加载关卡并绘制地图。加载它的图像就好了。

public class Level {
private Map map = null;
private ArrayList<Entity> entities = new ArrayList<Entity>();
private Player player = null;
private BufferedImage sourceImage = null;
private SpriteSheet sprites = null;

public Level(String file, Player player) {
    this.player = player;


    try {
        URL url = SpriteSheet.class.getResource(file);
        sourceImage = ImageIO.read(url);
    } catch (IOException e) {
        e.printStackTrace();
    }

    map = new Map(sourceImage, this.player);
    sprites = new SpriteSheet("/sheet.png", 8);
    entities.add(this.player);
}

Map.java&lt; ----这是主要问题所在。 Tile.png不是在JAR中寻求的,而是在外面寻找的。

public class Map {
private BufferedImage source = null;
private SpriteSheet sheet = null;
private Level level;

private Tile[][] tiles = null;
private int width = 0;
private int height = 0;

private Camera cam = null;
public int xOffset;
public int yOffset;

public Map(BufferedImage source, Entity cameraTarget) {
    cam = new Camera();
    cam.attach(cameraTarget);

    this.source = source;

    String is = ("/Tiles.png");     
    sheet = new SpriteSheet(is, 8);

    System.out.print(source +"");
    load();
}

和Gamescene.java&lt; ---加载它的图像就好了。

    public class GameScene extends Scene {
private Level level = null;
private Player player = null;

public GameScene() {
    player = new Player();
    level = new Level("/level.png", player);
}

如果需要,我可以给你更多代码。

1 个答案:

答案 0 :(得分:3)

猜猜。您首先使用大写首字母,并在您的代码中使用小写字母。也许这就是问题,因为Java内部区分大小写,但Windows不是(Linux再次出现)。看看案例的匹配情况。