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

Manipulating Images

The figure above shows how image data is created, behind the scenes. An image producer -- an object that adheres to the ImageProducer(in the Creating a User Interface trail)interface -- produces the raw data for an Image object. The image producer provides this data to an image consumer -- an object that adheres to the ImageConsumer(in the Creating a User Interface trail)interface. Unless you need to manipulate or create custom images, you don't usually need to know about image producers and consumers. The AWT automatically uses image producers and consumers behind the scenes.

The AWT supports image manipulation by letting you insert image filters between image producers and image consumers. 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. ImageFilter implements the ImageConsumer interface, since image filters intercept messages that the producer sends to the consumer. The following figure shows how an image filter sits between the image producer and consumer.

How to Use an Image Filter

Using an existing image filter is easy. You simply use the following code, modifying the image filter constructor as necessary:
Image sourceImage;
...//Initialize sourceImage, using the Toolkit or Applet getImage() method.
ImageFilter filter = new SomeImageFilter();
ImageProducer producer = new FilteredImageSource(sourceImage.getSource(),
                                                 filter);
Image resultImage = createImage(producer);
The following page explains how the above code works and tells you where to find some image filters.

How to Write an Image Filter

What if you can't find an image filter that does what you need? You can write your own image filter. This page gives you some tips on how to do so, including pointers to examples and an explanation of a custom filter that rotates images.


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