File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Java 2D, displaying an 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 "Java 2D, displaying an image" Watch "Java 2D, displaying an image" New topic
Author

Java 2D, displaying an image

Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
Hi all!

I'm having trouble displaying an image. I'm using a split pane, on the left side i have a tree with image names and paths to each image and on the right I would like to display the selected image from a tree in a JPanel. This is where I handle tree selection:



The function "showImage" should just put the image, defined with it's path (image.imagePath) and stored locally, on the left JPanel. I tried everything to show this image using Image, ImageIcon, BufferedImage, JAI and God knows what else but with none success. All imaegs are in TIF format. It would be useful if the shown image would be scaled to fit the panel and zoom controlls, using buttons, would also be very helpful.

Thanks in advance for all your help! Cheers
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
All imaegs are in TIF format
j2se up through 1.5 does not support the tiff image format. It does support jpg, png, gif, bmp and wbmp.
I believe that the JAI extension does support tiff format: see Chapter 4 in Programming in Java Advanced Imaging.
For more: Java Advanced Imaging.
I think you can download the ImageReader plug-ins to deal with specific image formats in the j2se sdk. Try JAI Image I/O Tools.
Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
I managed to show images from a tree to a split panel, using JAI, and scale it with the next code:



But I would still like to know what is the easiest way to enable dragging this image (instead of scrolling) and zooming into an image, so any help on those issues would be much appreciated.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The easiest way depends on what you want to do. With what you've given so far, if
DisplayJAI is a JComponent extension then you could drag and scale/zoom the image in this
components paintComponent method. The basic idea is to creat e Rectangle the size of the
image in the component. Draw the image at the origin of the Rectangle. In you mouse code
you can use the rectangle to test for mousePressed events in which you initiate a drag
episode. In the mouseDragged method you can reposition the rectangle and repaint the
component.
For the scaling you can use an AffineTransform to scale the image, or any designated
(clipped) part of it, and either scale it on the fly with g2.drawRenderedImage(image,
xform)
or make up a new BufferedImage by scaling the original and draw the new image.
I would be wary of using the Image.getScaledInstance method which can be slow (and
loads asynchronously) compared to scaling a BufferedImage.
Examples of both scaling and dragging images have been posted in this forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 2D, displaying an image
 
Similar Threads
Gett8ing focus when 2 JTrees in same panel.
Tree with Listener not working when added to ScrollPane
JTabbedPane
how can I change this tree with different Icons for Different nodes at Run Time
JTree and JScrollPane