那里可能有重复,但我似乎找不到一个来解决我的问题。
我的问题是我的 3 个图像文件中的 1 个不会从已编译的 JAR 文件中加载。我可以让它加载的唯一方法是图像就在 .jar 文件之外。
我的 3 个图像文件名:
Tiles.png <---One that only loads outside
Sheet.png
Level.png
SpriteSheet.java <---- 抓取精灵并给出图块大小
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 <---- 加载关卡并绘制地图。加载它的图像就好了。
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 <---- 这是主要问题所在。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 <---加载它的图像就好了。
public class GameScene extends Scene {
private Level level = null;
private Player player = null;
public GameScene() {
player = new Player();
level = new Level("/level.png", player);
}
如果需要,我可以给你更多代码。