Here I demonstrate a small module project that will enable to open Images within NetBeans. We often have to include image files in our projects. To select a proper image and its suitability we need the help of image viewer. But it would be nice if we can open the images from within the IDE. We will see how easily it can be done using NetBeans Module development and the Java Advanced Imaging API.(JAI) Detailed Information on JAI is present on the Official JAI Site.
2. Resources needed.
3. Setting up display.
4. Putting code in places.
JAI is intended to support image processing using the Java programming language as generally as possible. At the same time, JAI presents a simple programming model that can be readily used in applications without a tremendous mechanical programming overhead or a requirement that the programmer be expert in all phases of the API’s design. JAI encapsulates image data formats and remote method invocations within a reusable image data object, allowing an image file, a network image object, or a real-time data stream to be processed identically. Thus, JAI represents a simple programming model while concealing the complexity of the internal mechanisms.
2. Resources Needed:
I. NetBeans IDE 6.0/ 6.1Beta
II. JAI API
Download JAI API Builds
Install JAI API in appropriate directory. Make note of the path. We will need to add the JAR files in our libraries of project if it is not installed in proper directory of NetBeans.
You may want to refer this beginners tutorial before proceeding
3. Setting up Display:
I. Create a new NetBeans module project.
Name it as ImageModule.
II. Create a new Window Component.
We often have to include image files in our projects. To select a proper image and its suitability we need the help of image viewer. But it would be nice if we can open the images from within the IDE. We will see how easily it can be done using NetBeans Module development and the Java Advanced Imaging API.(JAI)
Detailed Information on JAI is present on the Official JAI Site.
- Set Window position as Editor.
- Give Class Name Prefix as: ImageFrame
- Select a suitable icon (16 x 16).
- Click Finish.
III. Go to ImageFrameTopComponent and add an internalFrame to it from the palette.
Rename it as ImageFrame.
IV. Add a MenuBar to the Frame.
Delete the Edit menu button.
Rename File Menu Button as Image.
V. Add a MenuItem to it as shown.
Edit its text to Open and set the variable name as openMenu.
V. Add an ActionPerformed event Handler to the Menu button.
OpenMenu >> Right Click >>Events >> ActionPerformed
4. Putting code in Places:
i. Import the following packages:
ii. Add private variables:
//To select the path of image file
private File file;
//To load the image file
private PlanarImage srcImage = null;
//To display the image file
private DisplayJAI srcdj = null;
private JScrollPane srcScrollPaneImage = null;
//To choose file by browsing
private JFileChooser FileChooser = new JFileChooser();
//This flag will be set once image is loaded for the first time
public boolean flag = false;
iii. Add the following method to the class:
public void displayimage(File path )
// Load the image which file name was passed as the first argument to the
srcImage = JAI.create("fileload", path.getPath());
//When image is loaded for first time...
if(flag == false )
// Get the JFrame's ContentPane.
Container contentPane = ImageFrame.getContentPane();
// Create an instance of DisplayJAI.
srcdj = new DisplayJAI(srcImage);
// Add to the JFrame's ContentPane an instance of JScrollPane containing the
// DisplayJAI instance.
srcScrollPaneImage = new JScrollPane(srcdj);
ImageFrame.setVisible(true); // show the frame.
flag = true;
//If any image was already loaded before
iv. In the OpenMenu Action Performed Handler add the following code.
It wil launch the JFileChooser dialog box so as to browse for image to be selected.
int returnVal = FileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
//Initialise file object
file = FileChooser.getSelectedFile();
//Display the image
V. Build and Run the project.
A new instance of the IDE will be launched. You can open the Image Frame as shown.
2. Choose the image using the file chooser. Image will get displayed as shown.
JAI is a powerful tool and can be efficiently used to design a Image viewing utility plug-in.