Adding image to JPanel through ImageIO.read?
Solution 1
Likely your image's file path isn't correct relative to the user directory. To find out where Java is starting to look, where the user directory is, place something like this line of code somewhere in your program:
System.out.println(System.getProperty("user.dir"));
Perhaps you'd be better off getting the image as an InputStream obtained from a resource and not as a file. e.g.,
image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
This will look for the image at the path given relative to the location of the class files, and in fact this is what you must do if your image is located in your jar file.
Edit 2
As an aside, often you need to first call the super's paintComponent method before doing any of your own drawing so as to allow necessary house-keeping can be done, such as getting rid of "dirty" image bits. e.g., change this:
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
to this:
public void paintComponent(Graphics g) {
super.paintComponent(g); // **** added****
g.drawImage(image, 0, 0, null);
}
Solution 2
I've written this ImagePanel class that i use for this scope :
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 7952119619331504986L;
private BufferedImage image;
public ImagePanel() { }
public ImagePanel(String resName) throws IOException {
loadFromResource(resName);
}
public ImagePanel(BufferedImage image) {
this.image = image;
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
/**
* Load the Image from a File
* @param path image name and path
* @throws IOException
*/
public void loadFromFile(String path) throws IOException {
image = ImageIO.read(new java.io.File(path));
}
/**
* Load Image from resource item
* @param resName name of the resource (e.g. : image.png)
* @throws IOException
*/
public void loadFromResource(String resName) throws IOException {
URL url = this.getClass().getResource(resName);
BufferedImage img = ImageIO.read(url);
image = img;
}
}
Then i use the ImagePanel in this way :
//Inizialization of the ImagePanel
ImagePanel panelImage = new ImagePanel();
//Try loading image into the image panel
try {
panelImage.loadFromResource("/Resources/someimage.png");
} catch (java.io.IOException e) {
//Handling Exception
}
Rakso
Awesome and passionate web developer, find me at http://oskarmendel.me/
Updated on June 05, 2022Comments
-
Rakso almost 2 years
I'm trying to add a JPanel with a picture in it. I'm using ImageIO.read to get the path but i get an IOException saying : can't read input file
The picture is called TCHLogo. It's a PNG inside a 'res' folder inside my project.
If there is any better way of displaying this image please also mention it!
Here is the code for my JPanel:
import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel; public class ImagePanel extends JPanel{ private BufferedImage image; public ImagePanel() { try { //THIS LINE BELLOW WAS ADDED image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png")); } catch (IOException ex) { // handle exception... System.out.println(ex); } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); //THIS LINE WAS ADDED g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters } }
Here is how i add the JPanel in my Applet:
ImagePanel appletRunningPanel; appletRunningPanel = new ImagePanel(); appletRunningPanel.setSize(300, 300); appletRunningPanel.validate(); add(appletRunningPanel);
EDIT I created a folder inside the bin which the application starts to look in currently.. the folder is called res and the picture is inside..
Now i get the following IOException when i run the line:
image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
Here is the error log:
java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(ImageIO.java:1338) at surprice.applet.ImagePanel.<init>(ImagePanel.java:17) at surprice.applet.MainClass.init(MainClass.java:41) at sun.applet.AppletPanel.run(AppletPanel.java:436) at java.lang.Thread.run(Thread.java:679)