Home > Articles > Programming > Java

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

A Rendering Example

Let's look at an example (see Listing 11.3) that illustrates how to use some of the rendering hints and how to add a border.

LISTING 11.3 The ImageFormatter class

import java.awt.*;
import java.util.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.renderable.*;
import javax.media.jai.operator.BorderDescriptor;
import javax.media.jai.*;

public class ImageFormatter extends app.JAISimpleViewer{
  protected Dimension border = new Dimension(10,10);

  public static void main(String[] args){
   ImageFormatter ip = new ImageFormatter();
   if(args.length <1) {
    System.out.println("Enter a valid image file name");
    System.exit(0);
   }
   ip.loadAndDisplay(args[0]);
  }

  public void loadAndDisplay(String filename){
   PlanarImage img = readAsPlanarImage(filename);
   TileCache tc = JAI.getDefaultInstance().getTileCache();
   System.out.println("Default Memory Capacity = "+ tc.getMemoryCapacity());
   tc.setMemoryCapacity(2024*2024);
   System.out.println("Memory Capacity = "+ tc.getMemoryCapacity());

   System.out.println("Without Border");
   System.out.println("———————");
   System.out.println("Size: "+
             img.getWidth()+ " , "+ img.getHeight());
   System.out.println("ULHC "+
             img.getMinX()+ " ,"+ img.getMinY());

   RenderedOp borderImg
      = com.vistech.jai.util.JAIUtil.setConstantBorder(img, border, 70);

   System.out.println("With Border");
   System.out.println("——————");
   System.out.println("Size: "+
             borderImg.getWidth()+ " ,"+ borderImg.getHeight());

   System.out.println("ULHC: "+
             borderImg.getMinX()+ " ,"+ borderImg.getMinY());

   RenderedOp opImage = reformatImage(borderImg, new Dimension(256, 256));
   displayImage(opImage);
  }

  public static RenderedOp reformatImage(PlanarImage img, Dimension tileDim) {
   ImageLayout tileLayout = new ImageLayout(img);
   tileLayout.setTileWidth(tileDim.width);
   tileLayout.setTileHeight(tileDim.width);

   HashMap map = new HashMap();
   map.put(JAI.KEY_IMAGE_LAYOUT, tileLayout);
   map.put(JAI.KEY_INTERPOLATION,
       Interpolation.getInstance(Interpolation.INTERP_BICUBIC));

   RenderingHints tileHints = new RenderingHints(map);
   ParameterBlock pb = new ParameterBlock();
   pb.addSource(img);
   return JAI.create("format", pb, tileHints);
  }

  public void launchFrame(PlanarImage img, int width, int height) {
   setTitle("JAI Image Viewer");
   viewer = new BorderedPanel(img);
   viewer.setPreferredSize(new Dimension(width, height));
   Container cp = getContentPane();
   getContentPane().setLayout(new GridLayout(1,1));
   cp.add(viewer);
   pack();
   setSize(new Dimension(width, height));
   show();
   viewer.repaint();
  }

  protected class BorderedPanel extends ImagePanel {

   public BorderedPanel(PlanarImage img){ super(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.setToScale(magx, magy);
       atx.translate(border.width, border.height);
     }
     if(image != null) g.drawRenderedImage(image, atx);
   }
  }
 }

The ImageFormatter class extends the JAISimpleViewer class that we developed in in Chapter 10 (see Listing 10.1). The loadAndDisplay() method first sets the tile cache and then adds a border. To add the border, loadAndDisplay() calls the setConstantBorder() method shown in Listing 11.2. Before the image is rendered, loadAndDisplay() calls the reformatImage() method to set the tile layout.

The loadAndDisplay() method has various print statements. When you run the application in Listing 11.3, you can see how the dimensions of the image change after the Border operator is applied. Figure 11.4 shows an image rendered with a constant border. To run the application, type "java app.ImageFormatter <image path>" on the command line.

FIGURE 11.4 An image displayed with a border

NOTE

The code for ImageFormatter is available on the book's Web page in the directory src/chapter11/format.

Here's an example of ImageFormatter output on the console:

Default Memory Capacity = 16777216
Memory Capacity = 4096576
Without Border
———————
Size: 208 ,222
ULHC 0 ,0
With Border
——————
Size: 228 ,242
ULHC: -10 ,-10

Notice that the size and the ULHC coordinates are changed after the border operation.

  • + Share This
  • 🔖 Save To Your Account