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();
		      g.drawImage(image,3,4,this);
	}
}
public  class Daj {
	    public static void main(String[] args){
		    Display d=new Display();
		    JFrame frame = new JFrame("hi");
		    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		    frame.getContentPane().add(d);
			frame.setSize(910,572);
			frame.setVisible(true);
	}

Recommended Answers

All 6 Replies

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

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.

just put the image file in the .class dir

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.