Previous | Next | Trail Map | Creating a User Interface | Working with Graphics


How to Use an Image Filter

The following applet uses a filter to rotate an image. The filter is a custom one named RotateFilter that you'll see discussed on the next page. All you need to know about the filter to use it is that its constructor takes a single double argument, the rotation angle in radians.

Here's the source code that uses the filter. (Here's the whole program.)

public class ImageRotator extends Applet {
    . . .
    RotatorCanvas rotator;
    double radiansPerDegree = Math.PI / 180;

    public void init() {
	//Load the image.
        Image image = getImage(getCodeBase(), "../images/pizza.gif");

	...//Create the component that uses the image filter:
	rotator = new RotatorCanvas(image);
	. . .
	add(rotator);
	. . .
    }

    public boolean action(Event evt, Object arg) {
	int degrees;

	...//Get the number of degrees to rotate the image by.

	//Convert to radians.
	rotator.rotateImage((double)degrees * radiansPerDegree);

	return true;
    }
}

class RotatorCanvas extends Canvas {
    Image sourceImage;
    Image resultImage;

    public RotatorCanvas(Image image) {
	sourceImage = image;
	resultImage = sourceImage;
    }

    public void rotateImage(double angle) {
	ImageFilter filter = new RotateFilter(angle);
	ImageProducer producer = new FilteredImageSource(
					sourceImage.getSource(),
					filter);
	resultImage = createImage(producer);
	repaint();
    }

    public void paint(Graphics g) {
	Dimension d = size();
	int x = (d.width - resultImage.getWidth(this)) / 2;
	int y = (d.height - resultImage.getHeight(this)) / 2;

	g.drawImage(resultImage, x, y, this); 
    }
}

How the Code Works

To use an image filter, a program must create an instance of the image filter, install it between an image producer and image consumer, and then get the filtered image from the image consumer. In the above applet, the RotatorCanvas rotateImage() method performs these tasks, using a slightly modified version of the code provided on the previous page. Here's a line by line explanation of how this code works.

First, the code instantiates the image filter by calling the filter's constructor. The code also initializes the image filter, if necessary.

ImageFilter filter = new RotateFilter(angle);
Next, the code creates an ImageProducer instance and connects the filter to it. The FilteredImageSource(in the API reference documentation) class is tailor-made for this purpose. The first argument to the FilteredImageSource constructor is an existing ImageProducer. You can get this from the image to be filtered using the Image getSource() method. The second argument to the FilteredImageSource constructor is the filter object.
ImageProducer producer = new FilteredImageSource(
				sourceImage.getSource(),
				filter);
Finally, the code causes the image to be filtered and gets the result, all by invoking the Component createImage() method. The lone argument to createImage() is the FilteredImageSource created in the above step.
resultImage = createImage(producer);
Here's what the path for the image data looks like:

Where to Find Image Filters

So where can you find existing image filters? The java.awt.image package includes one ready-to-use filter, CropImageFilter.(in the API reference documentation) You can also find several image filters used by applets at our website. All of the following pages include links to the source code for each applet and image filter: Finally, you can use the same image filter the above applet does: RotateFilter.


Previous | Next | Trail Map | Creating a User Interface | Working with Graphics