22

I've started Java a week ago, and now I would like to insert an image into my window. Whatever I try I keep having this in Eclipse: javax.imageio.IIOException: Can't read input file!

package graphics;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import src.Common;

public class Window extends JFrame
{
public class Panel extends JPanel
{

    public void paintComponent(Graphics g)
    {
        Image img; 
        try 
        {
        img = ImageIO.read(new File("/logo.jpg"));
        g.drawImage(img, 0, 0, this);
        } 
        catch (IOException e) 
        {
        e.printStackTrace();
        }

    }
}

public Window(String title, int width, int height)
{
    this.setTitle(title);
    this.setSize(width, height);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setContentPane(new Panel()); 
    this.setVisible(true);
}

}

I think the code is pretty self-explaining. I tried to solve the problem with this, this, and that .

What I'm trying to do is a desktop program, and my sources are stored like that : training/src/graphics/Window training/src/src/main

I did put the image I want to read in every folder, and still getting the issue :/

What did I do wrong?

EDIT Finally solved, here the answer

nIcE cOw gave me the link that helped. So I did put my images into a folder, and change the way to access to them, as described in the link.

getClass().getResource("/images/yourImageName.extension");
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
trolologuy
  • 1,900
  • 4
  • 21
  • 32
  • 1
    Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230). I hope it helps :-) To know how to access images, you can see the last link in that answer :-) – nIcE cOw Aug 19 '13 at 09:20
  • You can find more info regarding the same at the [info](http://stackoverflow.com/tags/embedded-resource/info) page of [tag:embedded-resource] tag :-) – nIcE cOw Aug 19 '13 at 11:17

4 Answers4

12

Have you tried using new File("logo.jpg"); (without the leading /)?

And are you sure, the logo.jpg is copied to your output? (Some IDEs don't copy every file from your source-directories to your output (or target) directories.)

/src
|-> Window.java
|-> Logo.jpg

becomes

/out
|-> Window.class

(Note that the IDE/compiler does not copy the image to your output-directory and so the compiled code cannot find the image - allthough you did specify the correct path)

deepthought-64
  • 566
  • 5
  • 16
  • Yup, i did try that though (should had mentionned it). I copied "Logo.jpg" Through the Windows user interface, not through eclipse. SO you mean, i should copy the images onto the output files from the IDE? how ? :/ – trolologuy Aug 19 '13 at 09:12
  • I think, eclipse copies the files automatically. Does your file has to be inside your project? If it is inside your project, you should try to load it with the classloader. (`this.getClass().getResource("images/beam.png")`) – deepthought-64 Aug 19 '13 at 09:39
6

Try do debug which file resource you actually try to access. First step would be to get your new File("/logo.jpg").get [Canonical]Path() and print it to System.out (or alternatively watch in the the debugger). I guess the problem is the / before logo.jpg, which points to your root directory (e.g. c:) and your file isn't there, but I don't know your file setup in detail.

LastFreeNickname
  • 1,445
  • 10
  • 17
  • When i try to print, it doesn't print anything :( I also tryed without the "/" before "Logo.jpg", but it doesn't change anything. My setup is : C:\Documents and Settings\USER\workspace\training\src\graphics – trolologuy Aug 19 '13 at 09:16
  • `new File(...).getPath()` will print something for sure. You can also try `new File()`, or `new File("")` if you are unsure. In case you don't see any output, try printing a random string as prefix (e.g. "my directory is ") to test your console. – LastFreeNickname Aug 19 '13 at 09:35
  • Finally, i've got the solution, i edited my first post, but i can't close the thread :o But thanks for the help ! – trolologuy Aug 19 '13 at 09:53
4

The problem is that you're looking at nothing before the image, so it's looking into a folder that isn't there to find it.

You have to create a folder to store the images in your project, and then call to it, your folder name in front of the image name. e.g.

ImageIO.read(new File("Folder/Image.png"));

Otherwise you can find the image by going through the entire directory, which isn't a good way as it takes longer, and when you move your project it won't be a working link as the directory will be different. For example:

ImageIO.read(new File("D:/eclipse/Workspace/Project/Folder/Image.png"));

Creating a folder in your project so its on the same level as the source folder in the directory and call to it for the image, like so:

Folder structure;

settings

src

Image Folder

bin

Community
  • 1
  • 1
Mrbull32
  • 41
  • 1
1

copypasting from a working project:

  private JPanel getPanel() {
    JPanel panel = new JPanel();
    JLabel labelLcl = new JLabel("Ye ana!");
    try {
        File f = new File("./src/res/0.jpg");
        //l.a(f.getCanonicalPath());
        BufferedImage imgLcl = ImageIO.read(f);
        ImageIcon iconLcl = new ImageIcon(imgLcl);
        labelLcl.setIcon(iconLcl );
        panel.add(labelLcl);
  } catch (IOException e) {
       e.printStackTrace();
  }
    return panel;
}

otherwise you can have folder/file read permission problem

CodeToLife
  • 3,672
  • 2
  • 41
  • 29