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.
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.
Joined: Jun 08, 2007
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.
Joined: Jan 14, 2004
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.