-1

I am currently learning java and I have problem and can't find anything about it. I got an "javax.imageio.IIOException: Can't read input file!" I don't understand what I did wrong : this is my code :

public class FirstWindoww extends JFrame  {
    JTextField usernameText = new JTextField(30);
    private JPanel panel1;
    private JPanel panel2;

    public FirstWindoww() {
        super("FROGGER GAME");
        JFrame frame = this;
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(400, 400);
        this.setLayout(new FlowLayout());
        panel1 = (JPanel) this.getContentPane();
        panel1.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
        panel1.setLayout(new FlowLayout());
        panel2 = (JPanel) this.getContentPane();
        panel1.setBackground(Color.GREEN);
        JPanel startingGame = new JPanel();
        this.setVisible(true);
        JLabel username = new JLabel("ENTER YOUR NAME");
        panel1.add(username);
        panel1.add(usernameText);
        usernameText.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
           System.out.println("Bouton cliqué !");
            }
               });

        JButton start = new JButton("START");
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.remove(panel1);
                frame.add(panel2);
                frame.validate();
            }
        });

        panel1.add(start);
        JButton exit = new JButton("EXIT");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        panel1.add(exit);
        JButton help = new JButton("HELP");
        help.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        panel1.add(help);
        try {
            // Load the background image

            BufferedImage img = ImageIO.read(new File("C:\\Users\\N\\Documents\\testGUI\\srce\\grenouille"));
            this.setContentPane(new JLabel(new ImageIcon(img)));
        } catch (IOException exp) {

            exp.printStackTrace();


        }
        JLabel imageLabel = new JLabel();
        imageLabel.setIcon(new ImageIcon("C:\\Users\\N\\Documents\\testGUI\\src\\grenouille.png"));
        panel1.add(imageLabel);

    }

Here are the error in a picture and you can see the fields on the right enter image description here

Thank you for taking the time to read this. Have a very good day everyone :) !!!

  • At the first image read `.png` is missing. Typically the file extensions are not shown in Windows. And srce/src – Joop Eggen Dec 03 '21 at 18:52
  • 1) For better help sooner, [edit] to add a [mre]. Don't post a picture of text, copy/paste the actual text (e.g. of the stack trace). Given the error mentions a 'line 73' in your code, it is apparent the MRE should be trimmed down from the current code. It should only take a handful of lines of code to demonstrate this problem. 2) Use `getResource` to obtain a `URL` pointing to the image. A `File` won't work at the time of deployment. **Edit:** The code also seems to be trying to remove and replace panels. Use a `CardLayout` instead. – Andrew Thompson Dec 03 '21 at 19:59
  • @AndrewThompson thank you for your answer. ok i'll make sure i take the right part of the code next time thanks for the advice. Where am I supposed to use getResource ? Inside imageIcon ? Yes I am trying to change th panel after a click on JButton but I tried multiple times it does not work I can't do it. I tried with CardLayout before – Jessica Robin Dec 03 '21 at 21:16
  • @JoopEggen thank you for answering me. Even with the .png added I got the same error – Jessica Robin Dec 03 '21 at 21:18

2 Answers2

0

Are you sure you have the correct path as well?

BufferedImage img = ImageIO.read(new File("C:\\Users\\N\\Documents\\testGUI\\srce\\grenouille"));

And

imageLabel.setIcon(new ImageIcon("C:\\Users\\N\\Documents\\testGUI\\src\\grenouille.png"));
Armel
  • 1
  • 1
  • Thank you for your answer. if I use `JLabel imageLabel = new JLabel(); imageLabel.setIcon(new ImageIcon("grenouille.png")); panel1.add(imageLabel);` it does not work – – Jessica Robin Dec 03 '21 at 21:05
  • @JessicaRobin But do you understand what `ImageIcon(String)` does? Do you know where it's looking for the file you've specified? @Armel is suggesting that you have a typo in you original path. I'd also recommend checking the result of `File#exists` to make sure that you do have the correct file reference – MadProgrammer Dec 03 '21 at 21:27
  • @ MadProgrammer But it does exist since I see the picture on my pannel when I run the code. I just got this error with it – Jessica Robin Dec 03 '21 at 21:42
0

Don't use absolute paths, like C:\Users\N\Documents\testGUI\src\grenouille, these are going to cause you no end of issues if you move the location of the program/image.

Also, looking at you path, C:\Users\N\Documents\testGUI\srce\grenouille, you seem to have a typo in src (ie srce) and you're missing the file extension off grenouille

One tip when trying to deal with these issues is to check the result of File#exits, this will give you a quick indication of possibly incorrect path laterals.

Instead, you should be focused on using "embedded resources", this allows the resource to be bundled with the binaries and they become more easily accessible at runtime.

The exact methods for doing this will come down to the IDE you are using and the workflow you use to build and package the project.

For Ant based Netbeans projects (and I believe Eclipse as well), you can place the resource directly under the src folder of the project. The build system will then include it in the resulting Jar file when you export the project. For Maven based projects, it's a bit more involved and I'm not going to discuss it here.

So, once you have the resource "embedded", you can simply use Class#getResource (or Class#getResourceAsStream depending on your needs), for example...

BufferedImage img = ImageIO.read(getClass().getResource("/grenouille.png")));

The location of the resource is relative to the top/root of the class path, in to make it simpler, think off it as offset from the top of the src directory (excluding src)

If you continue to have issues, you're going to have to diagnose the problem at your end. Start by doing a clean/build (or export of the project) and unzip the resulting Jar file and check its contents. If the resource doesn't exist, either the project isn't configured properly or the resource is in the wrong location.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366