This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to display an image in a Frame object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to display an image in a Frame object" Watch "How to display an image in a Frame object" New topic
Author

How to display an image in a Frame object

Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
hello all,

how can i paste or display an image inside a Frame object? codes that i usually see are image displayed along with Applets? is it possible on Frames..

thank you.

Cyrus
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Displaying an image is fairly easy. Pasting an image into an app is a little more complex. This example requires j2se 1.4+
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
sir

i will try your code..

thanks for the help
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
@sir craig wood,

i tried to use your code, i modified the line which details the filename of the image:

String fileName = "images/a1.jpg";

sir, should the java program and the image file be located on the same folder, Or should i create a new subfolder then copy the image on that folder.

My Directory Structure

drive c:
|----- java folder
++++++++++++|----- FrameImage.java
++++++++++++|----- images folder
++++++++++++++++++++++++|----- a1.jpg



it has returned this error:

Exception in thread "main" java.lang.IllegalArgumentException : input ==null!

at javac.imageio.ImageIO.read(Unknown Source)
at FrameImagePanel.loadImage(FrameImage.java:66)
at FrameImagePanel.<init>(FrameImage.java:40)
at FrameImage.<init>(FrameImage.java:13)
at FrameImage.main(FrameImage.java:30)



thanks sir..
[ March 02, 2005: Message edited by: Cyrus Serrano ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
should the java program and the image file be located on the same folder, Or should i create a new subfolder then copy the image on that folder
Either way is okay. The hardest part is to figure out how to get the image to display for the first time. The error indicates that the class loader could not find the image. You can add a System.out.println statement to check the url that the class loader is using to find the image, like this:

With the getResource method the image file needs to be on the classpath. Inside the same folder as your class files should be okay. You could also try constructing a full URL with a protocol of file:

although this can be tricky to get right. You just have to play around with it till you figure it out.

You can make up a small test application to experiment with this

Here's what I got when I ran it

As you can see the images folder is in the current directory (same as what you show in your last post). You can use the string printed to the console in the new URL constructor
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
sir craig

thanks a lot.. i've got it finally
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display an image in a Frame object
 
Similar Threads
awt
Image in JFrame ???
Frames in XML
Adding an image to a frame
Loading Images in a frame