Jeffrey Barneveld
Jeffrey Barneveld

Reputation: 184

java applet cant load more than 1 image

I got a problem im trying to make a java game in an applet.
I can't load more then 1 image, otherwise it will not load.
I am getting the images of the jar file.

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. And client wont even load.

It's giving the error: input == null

How to fix this.

Thank you

Upvotes: 0

Views: 128

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

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.

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.

Upvotes: 2

Related Questions