Search code examples
javaimagefilebufferedimagejapplet

Creating a buffered image in Java, can't read input file error


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.


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