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 :) !!!
CodePudding user response:
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"));
CodePudding user response:
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.