GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes problem in displaying image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in displaying image" Watch "problem in displaying image" New topic
Author

problem in displaying image

Nidhi Nagre
Ranch Hand

Joined: Nov 05, 2007
Posts: 64
hi all,

i am in little difficulty to display an image in applet sent from servlet using apache class library.applet is embedded in a jsp page.

firstly, the image is uploaded after making a connection between applet and servlet then image required to be display back in applet. the image has been sent successfuly to applet so far but it couldn't be displayed as icon of JLabel.

applet side code is shown below. here filePost is an object of PostMethod class.



the following snippet showing null while i pretend that userimg object is not null, it of type BufferedImage.



the code was working well, when image was buffered from local file system and applet was run in appletviewer only.

kindly make a look above and nail out the problem.

regards
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Nidhi Nagre:
i am in little difficulty to display an image in applet sent from servlet using apache class library.applet is embedded in a jsp page.


If the servlet can be accessed directly (or could temporarily become so), does the image show in a web browser? If so, it would seem to be a servlet issue and not a swing issue. (Also see item#2 below.)



two comments:

1) It's not wise to wait for a MediaTracker in paintComponent(). The idea is you should paint whatever part of the image is immediately available (perhaps nothing at all) and then schedule a repaint when the image has loaded. You don't want to tie up the event dispatch thread.

2) If you can use the ImageIcon constructor that takes a URL (as opposed to Image) then a MediaTracker is not necessary. ImageIcon takes care of loading for you. I would think you would be able to provide a URL that reqests the image from the servlet.

the following snippet showing null while i pretend that userimg object is not null, it of type BufferedImage.



I'm not sure what you mean by pretending. If userimg is null then there's an IOException which you should examine. If it's just imgdata.getDescription() that is null, then I wouldn't be concerned. It would only have a non-null description if you set one manually, or if the Image passed into the constructor has a String-valued comment property.

[edit: fix munged tags]
[ December 15, 2007: Message edited by: Brian Cole ]

bitguru blog
Nidhi Nagre
Ranch Hand

Joined: Nov 05, 2007
Posts: 64

Brian wrote:
It's not wise to wait for a MediaTracker in paintComponent().


would you please explain why MediaTracker should not be used. actually i am novice in java.

The idea is you should paint whatever part of the image is immediately available (perhaps nothing at all) and then schedule a repaint when the image has loaded. You don't want to tie up the event dispatch thread.


sould i put repaint() after graphics.drawimage() method. kindly clarify it again it would be better to put few snippet. and what is event dispatch thread?

i have tried removing paintComponent() block. and used this

but it is not working too. one more thing, i never been found any exception at any point in all the methods explained above. after running this i sat with a great patience with an anticipation that it might takes a time to load but nothing was seeming to happen.

Brian Wrote:
If you can use the ImageIcon constructor that takes a URL (as opposed to Image) then a MediaTracker is not necessary. ImageIcon takes care of loading for you. I would think you would be able to provide a URL that reqests the image from the servlet.



i tried it using URL for image directly but alway get a NPE.

image has been uploaded at servletContext.getRealPath(). in this case it is E:\java\UASProject\build\web\uploadedimage. here uploadedimage is the directry where image exist. the servlet classes exist in E:\java\UASProject\build\web\web-inf\classes directory.

servlet is deployed by http://localhost:8084/UASProject/storeimage and i tried loading image by http://localhost:8084/UASProject/uploadedimage/image.jpg.

but it not working. i dont know why? kindly give your points.

[ December 16, 2007: Message edited by: Nidhi Nagre ]
[ December 16, 2007: Message edited by: Nidhi Nagre ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Nidhi Nagre:
would you please explain why MediaTracker should not be used.


I didn't say MediaTracker shoudn't be used. I said that you shouldn't wait on one inside the paintComponent() method. That's because if paintComponent() does something that takes a long time (such as waiting on IO) the GUI will be frozen for the duration.

Waiting on a MediaTracker is fine elsewhere, though if you use an ImageIcon constructor that takes a filename or a URL then you don't have to because ImageIcon does the waiting for you.

i tried it using URL for image directly but alway get a NPE.


You don't say if the NPE occurs in your servlet or in your client.

servlet is deployed by http://localhost:8084/UASProject/storeimage and i tried loading image by http://localhost:8084/UASProject/uploadedimage/image.jpg.

but it not working. i dont know why?


I'm not a servlet expert but because you kept saying "uploaded" I was expecting a URL in a form closer to http://localhost:8084/UASProject/uploadedimage/image.jpg?id=123 but no matter. If you are expecting your servlet to serve the image at that URL and your web browser doesn't see the image at that URL then you have a servlet problem, not a swing problem.

You might want to focus on getting it to show in your web browser before worrying about your client code. You may also want to post for help on a Servlet forum instead of a Swing forum.
[ December 16, 2007: Message edited by: Brian Cole ]
 
 
subject: problem in displaying image