0

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

  • 1
    Just a little comment - in times like these it would be beneficial if your try-catch actually wrote out the exception it catches... – Mads Oct 16 '12 at 00:17
  • 2
    You should probably rethink that exception handling strategy. – Dmitri Oct 16 '12 at 00:19
  • 1) `G2D.drawImage(hud_right, 570, 0, null);` should be `G2D.drawImage(hud_right, 570, 0, this);` 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. 4) Are you **sure** the game players would like the game crammed into a web page? Free it by coding a frame based game and launching it using [JWS](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Oct 16 '12 at 04:20

1 Answers1

2

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366