Pages

Search

 

31 July 2009

[JAVA] Displaying Images with the DisplayJAI class

INTRODUCTION

DisplayJAI is a class distributed with the Java Advanced Imaging API that can display an instance of the RenderedImage class, including instances of PlanarImage and TiledImage (tiling is supported). Since it inherits from JComponent it can be used in graphical interfaces as any other component.

This chapter shows some basic usage examples. Other chapters in this section shows how to extend the class for more specific tasks.

Important: A message on this class' API documentation says: This class is not a committed part of the JavaTM Advanced Imaging API per se. It might therefore not be supported by JAI implementations other than that of Sun Microsystems, Inc.

Displaying Images with the DisplayJAI class

A simple application will demonstrate the usage of the DisplayJAI class. It will load an image from disk, create a simple graphical interface with the DisplayJAI on the center and a text label on the bottom. The instance of DisplayJAI will be contained inside a JScrollPane so if the image is larger than the application's window the user will be able to the scrollbars to display different viewports of the image. The text label will show information about the image.

CODE :

1 /*
2 * Part of the Java Image Processing Cookbook, please see
3 * http://www.lac.inpe.br/~rafael.santos/JIPCookbook/index.jsp
4 * for information on usage and distribution.
5 * Rafael Santos (rafael.santos@lac.inpe.br)
6 */
7 package display.basic;
8
9 import java.awt.BorderLayout;
10 import java.awt.Container;
11
12 import javax.media.jai.JAI;
13 import javax.media.jai.PlanarImage;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JScrollPane;
17
18 import com.sun.media.jai.widget.DisplayJAI;
19
20 /**
21 * This application shows how to use the DisplayJAI class with a JScrollPane to display images.
22 */
23 public class DisplayJAIExample
24 {
25 /**
26 * Entry point for this application.
27 * @param args an image file name.
28 */
29 public static void main(String[] args)
30 {
31 // Load the image which file name was passed as the first argument to the
32 // application.
33 PlanarImage image = JAI.create("fileload", args[0]);
34 // Get some information about the image
35 String imageInfo = "Dimensions: "+image.getWidth()+"x"+image.getHeight()+
36 " Bands:"+image.getNumBands();
37 // Create a frame for display.
38 JFrame frame = new JFrame();
39 frame.setTitle("DisplayJAI: "+args[0]);
40 // Get the JFrame's ContentPane.
41 Container contentPane = frame.getContentPane();
42 contentPane.setLayout(new BorderLayout());
43 // Create an instance of DisplayJAI.
44 DisplayJAI dj = new DisplayJAI(image);
45 // Add to the JFrame's ContentPane an instance of JScrollPane containing the
46 // DisplayJAI instance.
47 contentPane.add(new JScrollPane(dj),BorderLayout.CENTER);
48 // Add a text label with the image information.
49 contentPane.add(new JLabel(imageInfo),BorderLayout.SOUTH);
50 // Set the closing operation so the application is finished.
51 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52 frame.setSize(400,400); // adjust the frame size.
53 frame.setVisible(true); // show the frame.
54 }
55 }
Screenshot :

No comments: