简体   繁体   English

我无法将图像文件加载到JAR文件中

[英]I can't get an Image file to load inside the JAR file

There are probably duplicates of this out there but I can't seem to find one to solve my issue. 那里可能有重复的副​​本,但是我似乎找不到一个可以解决我的问题的副本。

My issue is that 1 of my 3 image files will not load from inside the compiled JAR file. 我的问题是我的3个图像文件中的1个不会从已编译的JAR文件内部加载。 The only way I can get it to load is if the image is just outside the .jar file. 我可以加载它的唯一方法是,如果图像位于.jar文件之外。

My 3 image file names: 我的3个图像文件名:

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

SpriteSheet.java <---- Grabs the sprite and give tile size SpriteSheet.java <----抓取Sprite并指定图块大小

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 <---- Loads the level and draws the map. Level.java <----加载关卡并绘制地图。 Loads its image just fine. 加载其图像就好了。

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 <---- This is where the main issue is. Map.java <----这是主要问题所在。 Tile.png isn't seeked in the JAR but outside. 不是在JAR中找到Tile.png,而是在外部。

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();
}

and Gamescene.java <---Loads its image just fine. 和Gamescene.java <---加载其图像就好了。

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

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

I can give you more code if needed. 如果需要,我可以给您更多代码。

Guess. 猜测。 You first use capital initials, and in your code small letters. 您首先使用大写字母缩写,然后在代码中使用小写字母。 So maybe that is the problem, as Java is internally case sensitive, but Windows is not (Linux again is). 也许这就是问题所在,因为Java在内部区分大小写,而Windows则不然(Linux再次如此)。 Look at the matching of the case. 看情况的匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM