• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to display an image in a Frame object

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir

i will try your code..

thanks for the help
 
Cyrus Serrano
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir craig

thanks a lot.. i've got it finally
 
reply
    Bookmark Topic Watch Topic
  • New Topic