문제

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