我在尝试在小程序中制作 Java 游戏时遇到问题。
我无法加载超过 1 个图像,否则它将无法加载。
我正在获取 jar 文件的图像。
代码加载器:
public BufferedImage LoadTex(String ura) {
BufferedImage res = null;
try {
URL url = this.getClass().getClassLoader().getResource("tex/" + ura);
res = ImageIO.read(url);
} catch (IOException e) {
}
return res;
}
代码小程序:
tex texu = new tex();
BufferedImage plr;
BufferedImage hud_right;
BufferedImage hud_bottom;
@Override
public void init() {
plr = texu.LoadTex("tspr.png");
hud_right = texu.LoadTex("hud_right.png");
hud_bottom = texu.LoadTex("hud_bottom.png");
}
@Override
public void paint(Graphics screen) {
Graphics2D G2D = (Graphics2D) screen;
G2D.drawImage(hud_right, 570, 0, null);
G2D.drawImage(hud_bottom, 0, 410, null);
}
它可以完美地处理一张图像,但如果我尝试更多它就会停止。 而且客户端甚至无法加载。
它给出了错误: 输入==空
如何解决这个问题。
谢谢
最佳答案
你不应该永远使用异常,至少你应该记录它们,这会节省你几个小时的时间...
public BufferedImage LoadTex(String ura) throws IOException {
BufferedImage res = null;
URL url = this.getClass().getClassLoader().getResource("tex/" + ura);
res = ImageIO.read(url);
return res;
}
您必须调用super.paint(g)
,paint方法在后台执行大量工作,您永远不应该忽略它。
public void paint(Graphics screen) {
super.paint(screen);
Graphics2D G2D = (Graphics2D) screen;
G2D.drawImage(hud_right, 570, 0, null);
G2D.drawImage(hud_bottom, 0, 410, null);
}
尝试单独加载每个图像并使每个图像都可以加载。如果此方法有效,但您仍然无法加载更多一张图像,则可能存在内存问题。
关于java小程序无法加载超过1张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12905776/