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.

有帮助吗?

解决方案

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

其他提示

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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top