繁体   English   中英

java applet无法加载超过1个图像

[英]java applet cant load more than 1 image

我试图在applet中制作一个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);
}

它与1张图片完美配合,但如果我尝试更多它停止。 客户端甚至不会加载。

它给出了错误:input == 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);
}

尝试单独加载每个图像并使每个图像都可以加载。 如果这样做并且您仍然无法加载一个图像,则可能存在内存问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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