Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

A JAI Image Browser

The Java browser application in Chapter 6 displays multiple images in a single frame and saves them as a single image. Let's modify that application to browse and save JAI images. We'll use the code in Listing 11.9 to aggregate multiple images into a single image.

There are two main classes: JAIImageBrowser and ViewerPanel. The JAIImageBrowser class code is similar to the ImageBrowser class code (see the section titled Image Browser: An Example of Aggregating Multiple Images in Chapter 6). JAIImageBrowser embeds the modified MultiImageLoader bean that can read image formats supported by JAI. When users select a set of images, JAIImageBrowser fires a planarImageLoaded event to the registered listeners. JAIImageBrowser receives this event, extracts the image set, and passes it on to ViewerPanel. Listing 11.10 shows the save methods of the modified ViewerPanel class.

LISTING 11.10 The ViewerPanel class

protected void save(){
  try{
   PlanarImage img = (PlanarImage) JAIUtil.aggregateImages(imageset,
                   new Dimension(cellWidth, cellHeight),
                   cols,rows);
   ImageSaverJAI.saveAsJPEG(img, "aggr.jpg");
  } catch(Exception e){}
}

For a detailed explanation of the ViewerPanel class, see the section titled Image Browser: An Example of Aggregating Multiple Images in Chapter 6. Figure 11.8 shows a screen shot of a frame with multiple images launched by the JAIImageBrowser application. Each image in this frame is drawn on an ImagePanel object (see Listing 11.11).

FIGURE 11.8 The image browser

LISTING 11.11 Displaying an image with a border

class ImagePanel extends JComponent {
  PlanarImage image;
  AffineTransform atx = new AffineTransform();
  int width, height;
  Dimension border = new Dimension(2,2);
  public ImagePanel(){ }
  public ImagePanel(PlanarImage img){
   RenderedOp op = JAIUtil.setConstantBorder(img,border,255);
   image = op.createInstance();
   width = image.getWidth();
   height = image.getHeight();
  }
  public void setImage(PlanarImage img){image = img;}

  public void paintComponent(Graphics gc){
   Graphics2D g = (Graphics2D)gc;
   Rectangle rect = this.getBounds();
   if((width != rect.width) || (height != rect.height)){
     double magx = rect.width/(double)width ;
     double magy = rect.height/(double)height ;
     atx.setToTranslation(border.width, border.height);
     atx.scale(magx, magy);
   }
   if(image != null) g.drawRenderedImage(image, atx);
  }
}

The ImagePanel class is a lightweight component for displaying images. The JAIImageBrowser object creates an ImagePanel object for each image.

When images are displayed side by side, a border is necessary to separate them. The ImagePanel constructor calls the method in Listing 11.11 to add the border to the input image.

The paintComponent() method translates the image so that the image border is visible on the left-hand side. It scales the image to fit the viewport and then draws it onto the graphical context.

  • + Share This
  • 🔖 Save To Your Account