'3D Pictures dont load Java

I tried to show some Blockarts in a 3D Generator. But i cant figure out this error:

javax.imageio.IIOException: Can't read input file!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1310)
at Texture.load(Texture.java:20)
at Texture.<init>(Texture.java:15)
at Texture.<clinit>(Texture.java:29)
at Game.<init>(Game.java:45)
at Game.main(Game.java:103)
javax.imageio.IIOException: Can't read input file!

When i start the Programm, the whole World appears with Black Blocks: Screen shot after Starting Programm

Im doing want to do a 3D engine, where you can just walk around in rooms. So this Blocks arent rendering for me.

This is my Code:

Class:Texture

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Texture {
  public int[] pixels;
  private String loc;
  public final int SIZE;

public Texture(String location, int SIZE) {
    loc = location;
    this.SIZE = SIZE;
    pixels = new int[SIZE * SIZE];
    load();
}

private void load() {
    try {
        BufferedImage image = ImageIO.read(new File(loc));
        int w = image.getWidth();
        int h = image.getHeight();
        image.getRGB(0, 0, w, h, pixels, 0, w);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static Texture wood = new Texture("block1.png", 64);
public static Texture brick = new Texture("block2.png", 64);
public static Texture bluestone = new Texture("block3.png", 64);
public static Texture stone = new Texture("block1.png", 64);

}

Class Game:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.ArrayList;
import javax.swing.JFrame;

public class Game extends JFrame implements Runnable{

 private static final long serialVersionUID = 1L;
 public int mapWidth = 15;
 public int mapHeight = 15;
 private Thread thread;
 private boolean running;
 private BufferedImage image;
 public int[] pixels;
 public ArrayList<Texture> textures;
 public Camera camera;
 public Screen screen;
 public static int[][] map =
        {
               {1,1,1,1,1,1,1,1,2,2,2,2,2,2,2},
               {1,0,0,0,0,0,0,0,2,0,0,0,0,0,2},
               {1,0,3,3,3,3,3,0,0,0,0,0,0,0,2},
               {1,0,3,0,0,0,3,0,2,0,0,0,0,0,2},
               {1,0,3,0,0,0,3,0,2,2,2,0,2,2,2},
               {1,0,3,0,0,0,3,0,2,0,0,0,0,0,2},
               {1,0,3,3,0,3,3,0,2,0,0,0,0,0,2},
               {1,0,0,0,0,0,0,0,2,0,0,0,0,0,2},
               {1,1,1,1,1,1,1,1,4,4,4,0,4,4,4},
               {1,0,0,0,0,0,1,4,0,0,0,0,0,0,4},
               {1,0,0,0,0,0,1,4,0,0,0,0,0,0,4},
               {1,0,0,0,0,0,1,4,0,3,3,3,3,0,4},
               {1,0,0,0,0,0,1,4,0,3,3,3,3,0,4},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
               {1,1,1,1,1,1,1,4,4,4,4,4,4,4,4}

        };
public Game() {
    thread = new Thread(this);
    image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
    textures = new ArrayList<Texture>();
    textures.add(Texture.wood);
    textures.add(Texture.brick);
    textures.add(Texture.bluestone);
    textures.add(Texture.stone);
    camera = new Camera(4.5, 4.5, 1, 0, 0, -.66);
    screen = new Screen(map, mapWidth, mapHeight, textures, 640, 480);
    addKeyListener(camera);
    setSize(640, 480);
    setResizable(false);
    setTitle("3D Engine");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.black);
    setLocationRelativeTo(null);
    setVisible(true);
    start();
}
private synchronized void start() {
    running = true;
    thread.start();
}
public synchronized void stop() {
    running = false;
    try {
        thread.join();
    } catch(InterruptedException e) {
        e.printStackTrace();
    }
}
public void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null) {
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
    bs.show();
}
public void run() {
    long lastTime = System.nanoTime();
    final double ns = 1000000000.0 / 60.0;//60 times per second
    double delta = 0;
    requestFocus();
    while(running) {
        long now = System.nanoTime();
        delta = delta + ((now-lastTime) / ns);
        lastTime = now;
        while (delta >= 1)//Make sure update is only happening 60 times a second
        {
            //handles all of the logic restricted time
            screen.update(camera, pixels);
            camera.update(map);
            delta--;
        }
        render();//displays to the screen unrestricted time
    }
}
public static void main(String [] args) {
    Game game = new Game();
}

}



Solution 1:[1]

You have a problem loading your texture. Assess how to handle the root cause (why can the image not be loaded?) and the consequences (your world is black):

Root cause

To find out why the image cannot be loaded, one measure would be to know what image the application is trying to load. Modify the load() method like so:

private void load() {
    File file = new File(loc);
    try {
        BufferedImage image = ImageIO.read(file);
        int w = image.getWidth();
        int h = image.getHeight();
        image.getRGB(0, 0, w, h, pixels, 0, w);
    } catch (IOException e) {
        System.out.println("ERROR: Could not load "+file.getAbsolutePath());
        e.printStackTrace();
    }
}

You may find that you are searching just in the wrong directory. Or the access privileges are not set correctly. Or the file content is corrupt. Or...

Impact

When your load method fails, it does not stop the program. Instead, your game keeps running. However the pixels array does not contain good data.

Ensure you have meaningful emergency defaults in case the method fails. You could load an 'error image' into the pixels array. With that you should at least not end up with a black window.

Solution 2:[2]

The exception stack trace is pretty straight-forward, and should tell you what you need to do. It points to a single exception in the code of ImageIO.read(File) which look like this:

public static BufferedImage read(File input) throws IOException {
// ... 
    if (!input.canRead()) {
        throw new IIOException("Can't read input file!"); // <-- This is the one
    }
// ... 
}

As you can see, the problem is that canRead() returns false, and according to the method's API documentation, this method returns "true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise".

Either your file does not exist at the location you try to read from, or the file has access protection that prohibits your application to read it. It's that simple.

I believe that printing new File(loc).getAbsolutePath() in the catch block will help you debug the actual location and why things don't work. Most likely, you should not use a File at all, but instead use an embedded resource in your application.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Harald K