简体   繁体   English

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

[英]java applet cant load more than 1 image

I got a problem im trying to make a java game in an applet. 我试图在applet中制作一个java游戏时遇到了问题。
I can't load more then 1 image, otherwise it will not load. 我无法加载超过1个图像,否则将无法加载。
I am getting the images of the jar file. 我正在获取jar文件的图像。

Code loader: 代码加载器:

    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;
    }

Code applet: 代码小程序:

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);
}

It works perfect with 1 image but if i try more it stops. 它与1张图片完美配合,但如果我尝试更多它停止。 And client wont even load. 客户端甚至不会加载。

It's giving the error: input == null 它给出了错误:input == null

How to fix this. 如何解决这个问题。

Thank you 谢谢

You should NEVER consume exceptions, at the very least you should log them, it will save you hours of hair pulling... 永远不应该消费异常,至少你应该记录它们,这将节省你的头发拉动时间......

public BufferedImage LoadTex(String ura) throws IOException {
    BufferedImage res = null;
    URL url = this.getClass().getClassLoader().getResource("tex/" + ura);
    res = ImageIO.read(url);
    return res;
}

You MUST call super.paint(g) , the paint method does a great deal of work in the background and you should never ignore it. 必须调用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 loading each image individually and make each image can load. 尝试单独加载每个图像并使每个图像都可以加载。 If this works and you still can't load more the one image, you may have a memory issue. 如果这样做并且您仍然无法加载一个图像,则可能存在内存问题。

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

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