Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java小程序无法加载多个图像_Java_Javax.imageio - Fatal编程技术网

java小程序无法加载多个图像

java小程序无法加载多个图像,java,javax.imageio,Java,Javax.imageio,我遇到了一个问题,我试图在小程序中制作java游戏。 我无法加载超过1个图像,否则将无法加载。 我正在获取jar文件的图像。 代码加载器: public BufferedImage LoadTex(String ura) { BufferedImage res = null; try { URL url = this.getClass().getClassLoader().getResource("tex/" + ura);

我遇到了一个问题,我试图在小程序中制作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);
}
它可以完美地与一个图像,但如果我尝试更多它停止。 客户端甚至不会加载

它给出了一个错误: 输入==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);
}

尝试分别加载每个图像,并使每个图像都可以加载。如果这样做有效,您仍然无法加载更多的一个图像,您可能会有内存问题。

只是一点注释-在这种情况下,如果您的try-catch实际写出了它捕获的异常,这将是有益的…您可能应该重新考虑异常处理策略。1)
G2D.drawImage(hud_right,570,0,null)应该是
G2D.drawImage(hud_right,570,0,this)2)若要更快获得更好的帮助,请发布一个。3) 请学习类、方法和属性名称的通用(特别是用于名称的大小写)并一致使用。4) 您确定游戏玩家希望将游戏塞进网页吗?通过对基于帧的游戏进行编码并使用启动它来释放它。您已确保可以单独加载每个图像?问题是我加载了错误的图像o。o感谢帮助:)