. Advertisement .
..3..
. Advertisement .
..4..
I’m a new in Java. I encounter the ”javax.imageio.iioexception: can’t read input file!” error when I want to insert an image into my window.
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 tried putting the image I wanted to read in each folder, but the problem still occurs.
what went wrong with me?
The cause: The issue is that nothing is shown before the image, so your computer is searching for it in a folder that doesn’t exist.
Solution: You must first create the folder to store the images in your project and then place the folder name in front of the image name.
e.g.