plz help me out ...
i m using the following code to display an image which resides on my pc (C:\Users\vivek\Pictures\firefox,JPG)... but when i run the code ....there is no compile time error..n also not any run time error..n the output is a empty frame :'(

import java.awt.*;
import javax.swing.*;
class Display  extends JPanel{
        	public void paintComponent(Graphics g){
	         Image image = new ImageIcon("firefox.JPG").getImage();
public  class Daj {
	    public static void main(String[] args){
		    Display d=new Display();
		    JFrame frame = new JFrame("hi");
7 Years
Discussion Span
Last Post by painless

It will look for your file in the default location - probably C:\Users\vivek, it's not smart enough to guess that you want the Pictures sub-dir. The only way to be sure is to specify the full absolute path when referring to a file


As there is no checking before you attempt to read image and ImageIcon will not throw any errors you need to make sure that provided resource does exists. This tutorial should solve your problem.

PS: In the future when posting any code please use code tags

Edited by happygeek: fixed formatting


Wrap up source code with BB code tags.
As per JC's suggestion; use physical path of picture file or location of both, picture and .class file must be a same (path or directory).

PS: also verify the name and extension of picture file.


adatapost: can you please clarify your comment "... or location of both, picture and .class file must be a same (path or directory)."?

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
This is not the same as the classpath.


It will look for your file in the default location - probably C:\Users\vivek,...

Sorry, this is probably wrong. Momentary confusion on my part between user.dir and user.home. Default location is the current directory at the time/place when the java/javaw command was executed. The following points still stand.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.