• Post Reply Bookmark Topic Watch Topic
  • New Topic

jpg image viewer  RSS feed

 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to modify code that displays a single image that I found on the web (this works fine). I want to enhance it so that using a menu item, I can select a new jpg file from a chooser and have that replace the original image. My code is not responsive to the new file chosen -- the original image is not replaced, but continues to be displayed. I have also tried a version of my prototype incorporating the JFrame and associated menu into the primary code instead of the "main" method and I get the same result.
Should I be instantiating LoadImageApp and adding it to the JFrame only once and then change the image with a method?
Do I have to close the file after reading? How?
Do I have to delete the previous instance of LoadImageApp from the JFrame before adding a new instance? How?
Any help and guidance would be appreciated.

jpg Images
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first part of that code is outdated AWT stuff. And you shouldn't mix AWT and Swing components in the same GUI. With Swing, all you need to do is use a JLabel with an ImageIcon. Either make a new Icon and call setIcon(...) or setImage(...) to the Icon and call repaint() on the label (and revalidate() its container if the new image is a different size).

These tutorials will help you:
http://docs.oracle.com/javase/tutorial/uiswing/components/label.html
http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html
http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestion -- I will follow up.

My ultimate goal is to make a specialized photo image editor. Since I will want to be able to modify the image, is Icon still the way to go? The documentation seems to indicate that an icon is fixed. Or is it that all the manipulation of the image would take place in a buffered image and the icon is used only to display?

Thanks
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. An ImageIcon contains an Image; a BufferedImage is-a Image. But if your image is to be loaded from a file, first load it using ImageIO#read(...) and construct the ImageIcon with the image as argument. An ImageIcon constructed with a String path to the file loads the image using java.awt.Toolkit, which caches the image as a result of which updates/changes/edits may not be reflected.

After making any changes to (aka editing) the BufferedImage, you *will* need to repaint() the JLabel that displays the Icon.
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for your inputs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!