| Author |
How to load an image to display
|
John McDonald
Ranch Hand
Joined: Jul 01, 2003
Posts: 112
|
|
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
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
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
Joined: Jul 01, 2003
Posts: 112
|
|
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 ]
|
 |
George Kalfas
Greenhorn
Joined: Aug 05, 2004
Posts: 22
|
|
I have another question based on that. If we were using strictly AWT what would we do in this case?
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
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
Joined: Jul 01, 2003
Posts: 112
|
|
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
Joined: Jan 14, 2004
Posts: 1535
|
|
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
Joined: Jul 01, 2003
Posts: 112
|
|
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
Joined: Jan 14, 2004
Posts: 1535
|
|
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.
|
 |
 |
|
|
subject: How to load an image to display
|
|
|