Working with Graphics |
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 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 interface, which lets it get data from an image producer. An image filter is an ImageFilter 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]:So where can you find existing image filters? The java.awt.image package includes one ready-to-use filter, CropImageFilter. 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:Image sourceImage = getImage(url); ImageFilter filter = new SomeImageFilter(); ImageProducer producer = new FilteredImageSource(sourceImage.getSource(), filter); Image resultImage = createImage(producer);
- The Dynamically Generated Color Bullets page contains two applets that modify the color of an image. The first applet, AlphaBullet, defines and uses an AlphaColorFilter; the second, HueBullet, defines and uses a HueFilter.
- The Live Feedback ImageMap page demonstrates an image mapping applet. It uses many filters to provide visual feedback when the cursor moves over certain areas or the user clicks [check] a special area.
- The Image Test page performs a variety of image processing. Besides letting the user scale and move the image, it defines and uses two filters. The AlphaFilter makes the image transparent, and the RedBlueSwapFilter changes the colors in the image.
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 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. 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.]
Working with Graphics |