简体   繁体   中英

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. The only way I can get it to load is if the image is just outside the .jar file.

My 3 image file names:

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

SpriteSheet.java <---- Grabs the sprite and give tile size

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. 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. Tile.png isn't seeked in the JAR but outside.

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.

    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). Look at the matching of the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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