我想在我的RPG游戏中添加一些层次。但这失败了:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class TileSet {
public static final int TILEWIDTH = 64, TILEHEIGHT = 64;
private BufferedImage[] tiles;
public TileSet(String path, int sizeX, int sizeY) {
tiles = new BufferedImage[sizeX * sizeY];
BufferedImage tileSet;
try {
tileSet = ImageIO.read(TileSet.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
return;
}
int i = 0;
for(int y = 0; y < sizeY; y++) {
for(int x = 0; x < sizeX; x++) {
tiles[i++] = tileSet.getSubimage(x * (TILEWIDTH + 3), y * (TILEHEIGHT + 3), TILEWIDTH, TILEHEIGHT);
}
}
}
public void renderTile(Graphics g, int tileNum, int x, int y){
g.drawImage(tiles[tileNum], x, y, TILEWIDTH, TILEHEIGHT, null);
}
}
因此,问题出现在此行:
tiles[i++] = tileSet.getSubimage(x * (TILEWIDTH + 3), y * (TILEHEIGHT + 3), TILEWIDTH, TILEHEIGHT);
确切的错误是:
线程“ Thread-0”中的异常java.awt.image.RasterFormatException: [x +宽度)在Raster的外部 sun.awt.image.ByteInterleavedRaster.createWritableChild(未知 来源)java.awt.image.BufferedImage.getSubimage(未知来源) 在de.gandalf1783.TileSet。(TileSet.java:25)在 java.lang.Thread.run(未知)上的de.gandalf1783.Game.run(Game.java:57) 来源)