File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java 2D, displaying an image" Watch "Java 2D, displaying an image" New topic

Java 2D, displaying an image

Ivan Bosnjak

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

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
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,
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:
subject: Java 2D, displaying an image
It's not a secret anymore!