Home > Software engineering >  Why is there an error when I try to load an image?
Why is there an error when I try to load an image?

Time:01-06

So, I have made a class called TileDrawer.java, which is supposed to load in a BufferedImage with the following code:

BufferedImage spriteSheet = null;    

public void initSpriteSheet() {
        try {
                this.spriteSheet = ImageIO.read(new File("resources\\spritesheet.png"));
        } catch (IOException e) {
                e.printStackTrace();
        }
}   

but when I try to run the method after initializing the object, it gives me this error:

javax.imageio.IIOException: Can't read input file!

This is my current full code for the TileDrawer class:

package mainGame;
        
//imports
                
public class TileDrawer {
        BufferedImage spriteSheet = null;    
                
         
        public void initSpriteSheet() {
                try {
                        this.spriteSheet = ImageIO.read(new File("resources\\spritesheet.png"));
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }        
        
        //irrelevant methods were here
}

and I also have this class, called GamePanel with the following code:

package mainGame;

//imports

public class GamePanel extends JPanel implements Runnable {
        
        Thread gameThread;
        
        TileDrawer tileDrawer = new TileDrawer();

        //vars
        
        public GamePanel() {} //game panel
        
        public void startGameThread() {
                tileDrawer.initSpriteSheet(); //  <---  here                                                     
                gameThread = new Thread(this);
                gameThread.start();
        }
                 
        @Override
        public void run() {
                //stuff to make a functioning loop

                while (gameThread != null) {
                        
                        //more loop stuff

                        if (delta >= 1) { //delta is only used to cap the framerate

                                update();
                                repaint();
                                
                                //even more loop stuff
                        }
                }
        }

        public void update() {
                //update stuff here
        }

        protected void paintComponent(Graphics g) {

                super.paintComponent(g);

                Graphics2D g2 = (Graphics2D)g;

                //drawing stuff goes here

                g2.dispose();

        }
}

Some background info:

  • I am using Debian Linux
  • The directory looks like this:
MyGame
|------build
|      |------(Compiled java files)
|------src
|      |------Main.java (irrelevant)
|      |------GamePanel.java
|      |------TileDrawer.java
|      |------resources
|      |      |------spriteSheet.png
  • I am not using an IDE to compile my code, I am simply using these commands:
cd /home/{my username}/MyGame/
javac -d ./build src/Main.java src/GamePanel.java src/TileDrawer.java
cd build
java mainGame.Main

All of these things I tried, but none of them worked when I tried to implement them:

  • changing the file path into singular normal slashes
  • changing the file path into singular normal slashes and putting one at the front
  • using a normal Image file instead of a BufferedImage
  • putting the image file into the same directory as the java file

Any help on how to fix my problem would be greatly appreciated.

CodePudding user response:

Thanks everyone for all the help! If anyone sees this in the future, here is what I changed to make it work.

First, I moved the resource folder out of the src folder and into the root folder so it could be accessed (thanks to Jorge Campos for that).

Then, I loaded in the image as a resource instead of a file, and I fixed the capitalization error. (thanks to Hovercraft Full Of Eels and 67af7af3-67f3-48bf-98c5-d9155c for that, respectively).

In the URL, I changed the \\'s into /'s, and added one in front of it.

In total, I changed this:

this.spriteSheet = ImageIO.read(new File("resources\\spritesheet.png"));

Into this:

this.spriteSheet = ImageIO.read(getClass().getResourceAsStream("/resources/spriteSheet.png"));

and I moved the resources folder into the root folder.

Once again, thank you all for your help!

note: the 'this.' in the above code examples in unnecessary, but it works either way.

  • Related