Question

I'm making a JApplet and attempting to create a buffered image in java, then to draw subimages of it on each half of the screen (this is a 2 player game).

The Declaration is:

public File map = new File("Resources/fullmap.png");
public BufferedImage fullmap;

fullmap.png is the Image of the entire map I'm using and it's in my resources folder.

I then try to initialize the BufferedImage and create the subimage by doing this:

try { 
            fullmap = ImageIO.read(map);
        } catch (IOException ex) {
            Logger.getLogger(Tankgame.class.getName()).log(Level.SEVERE, null, ex);
        }

Image drawP1Side = fullmap.getSubimage(p1.x, p1.y, w/2, h/2);

However I get an error that says "Can't read input file". Am I initializing my file "map" incorrectly? Any help is appreciated, thanks.

Was it helpful?

Solution

If it is Can't read input file then it can be problem with your image path. Try using

public File map = new File("/Resources/fullmap.png");

OTHER TIPS

The java docs state that inside an applet, you have to do something slightly different:

If the code is running in an applet, then its just as easy to obtain the image from the applet codebase. The following excerpt is from LoadImageApplet.java:

try {
    URL url = new URL(getCodeBase(), "Resources/fullmap.png");
    fullmap = ImageIO.read(url);
} catch (IOException e) {
        Logger.getLogger(Tankgame.class.getName()).log(Level.SEVERE, null, ex);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top