• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to load an image to display

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I would like to write a piece of code to load an image and display without using applet. Is that possible? Thanks.

John
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lots of options when it comes to loading and displaying images. Here's an easy one. The image is located in the imagesfolder which is located in the same folder as the DisplayAnImage class file.
 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. I provided a very detailed answer. One more thing, I don't have the tiff file yet. Can it handle tiff file in similar manner jpeg? Thanks again

John
I forgot one detail in your response.
My image file is a large image like a graphic document. You used ImageIcon hold the image. Would that be O.K.?
[ August 05, 2004: Message edited by: John McDonald ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another question based on that. If we were using strictly AWT what would we do in this case?
 
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
Here's a link to an earlier post Supported image formats from this forum that may be helpful for the tiff question. For the image size in the JLabel(ImageIcon) I would try and see if it works.

And here's a demo of how to diaplay an image in the AWT.

[ August 07, 2004: Message edited by: Craig Wood ]
 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am back. I would like to ask how do make sure the image loaded sucessfully in case jpg file does not exist or it is corrupted. Any hint is greatly appreciated. Thanks.

John
 
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
The loadImage method below should let you know if there is trouble. ImageIO is new in j2se 1.4 and offers other ways to get to the image data, eg, via the File api (see read methods in ImageIO api).

The ImageIcon (j2se 1.2+) class doesn't seem to give any notice of trouble. One way to peek under the covers relies on the fact that the Component class implements the ImageObserver interface which has the single method imageUpdate. So if you load the image inside a Component you can override the imageUpdate method and use it to monitor the image loading process and/or check the result.

In the AWT you can either use the ImageObserver interface as above or work directly with methods in the MediaTracker class to monitor the image loading process.
 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Craig,
Thanks again for continuing answering my question. It works fine for the local images. But I have a images file from other location, it can't get the source and throw the exception, of course, eventhough I define an absolute path.

John
 
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
getResource works for files on the classpath, including files inside jar files for j2se 1.4+. For network URLs you could try constructing a URL from the absolute path.
reply
    Bookmark Topic Watch Topic
  • New Topic