Java Reference
In-Depth Information
Code 11.11
Abstract class Filter :
Superclass for all filters
* @param image The image to be changed by this filter.
public abstract void apply(OFImage image);
Once we have written the superclass, it is not hard to implement specific filters as subclasses. All we
need to do is provide an implementation for the apply method that manipulates an image (passed in
as a parameter) using its getPixel and setPixel methods. Code 11.12 shows an example.
Code 11.12
Implementation of a
specific filter class
// All comments omitted.
public class DarkerFilter extends Filter
public DarkerFilter(String name)
super (name);
public void apply(OFImage image)
int height = image.getHeight();
int width = image.getWidth();
for ( int y = 0; y < height; y++) {
for ( int x = 0; x < width; x++) {
x, y, image.getPixel(x, y).darker());
As a side effect of this, the OFImage class becomes much simpler, as all of the filter methods
can be removed from it. It now only defines the setPixel and getPixel methods.
Once we have defined our filters like this, we can create filter objects and store them in a
collection (Code 11.13).
Code 11.13
Adding a collection
of filters
public class ImageViewer
// Other fields and comments omitted.
private List<Filter> filters;
Search WWH ::

Custom Search