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


Manipulating Images

The AWT supports image manipulation by letting you insert image filters between image producers and image consumers. An image producer is an object that adheres to the ImageProducer(in the Creating a User Interface trail) interface and can produce the raw data for an Image object. Each image producer can provide data to one or more image consumers. An image consumer is an object that adheres to the ImageConsumer(in the Creating a User Interface trail) interface, which lets it get data from an image producer. An image filter is an ImageFilter(in the Creating a User Interface trail) object that sits between a producer and a consumer, modifying the image data before the consumer gets it.

[Show a figure with ImageProducer, ImageConsumer, ImageFilter.]

Unless you need to manipulate images [or create them?], you don't usually need to know about image producers and consumers, much less filters. The AWT automatically uses image producers and consumers behind the scenes.

How to Use an Image Filter

Using an existing image filter is easy. Here's an example [should provide full program listing + explain the following code + include applet that displays before and after images]:
Image sourceImage = getImage(url);
ImageFilter filter = new SomeImageFilter();
ImageProducer producer = new FilteredImageSource(sourceImage.getSource(),
						 filter);
Image resultImage = createImage(producer);
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:

How to Write an Image Filter

What if you can't find an image filter that does what you need? It's not too difficult to write your own image filter. All image filters should be implemented as subclasses of the ImageFilter(in the Creating a User Interface trail) class.

If your image filter will be modifying the colors or transparency of an image, you'll probably want to create a subclass of RGBImageFilter.(in the Creating a User Interface trail) You can find examples of RGBImageFilter subclasses in the applets mentioned above (Dynamically Generated Color Bullets, Live Feedback ImageMap, and Image Test).

The following applet defines and uses a RotateImageFilter -- a class that extends the ImageFilter class directly. [Explain the applet.]

[INCLUDE AN APPLET and source code that displays an image and lets you type in the amount to rotate it by.]


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