Java Q&A
by Aaron Michael Cohen 

Example 1: 

public boolean imageUpdate( Image img, int infoflags, int x, int y, int width, int height);


Example 2:

(a) 
void setPixels( int x, int y, int w, int h, ColorModel cm, byte[] pixels, int offset, int scansize);

(b)
void setPixels( int x, int y, int w, int h, ColorModel cm, int[] pixels, int offset, int scansize);


Example 3: 

Image big_image = getDefaultToolkit.getImage("lena.jpg");
ImageFilter shrinker=new AreaAveragingScaleFilter(big_image.getWidth()/2, 
                                                  big_image.getHeight() / 2);
ImageProducer source=new FilteredImageSource(big_image.getSource(),shrinker);
Image small_image=createImage(source);


Listing One
import java.awt.Color;
import java.awt.image.*;

public class GreyOutImageFilter extends RGBImageFilter {
    // save the int value of the grey color...
    protected int greyOutValue = Color.gray.getRGB();
    public GreyOutImageFilter() {
        // this filter is position dependent, so we can't filter
        // by just changing the color table...
        canFilterIndexColorModel = false;
    }        
    public int filterRGB( int x, int y, int rgb) {
        // set every other pixel to grey using a checkerboard pattern...
        if (((x ^ y) & 1) == 0) {
            return greyOutValue;
        }
        else {
            return rgb;
        }
    }        
}    

Listing Two
import java.awt.image.*;

public class RotateClockwiseImageFilter extends ImageFilter {
    protected int srcwidth, srcheight;
    protected int destwidth, destheight;
    public void setDimensions( int width, int height) {
        // source height becomes destination width and vice-versa...
        this.destwidth = height;
        this.destheight = width;
        
        // tell the consumer the size of the image that we will be sending...
        super.setDimensions( this.destwidth, this.destheight);
    }
    public void setHints( int hints) {
        // because this filter delivers pixels a scan column at a time, 
        // we need to clear COMPLETESCANLINES and TOPDOWNLEFTRIGHT hint 
        // bits and set the RANDOMPIXELORDER bit...
        hints = (hints & (~COMPLETESCANLINES) & 
                                (~TOPDOWNLEFTRIGHT)) | RANDOMPIXELORDER; 
        super.setHints(hints);
    }        
    public void setPixels( int x, int y, int w, int h, ColorModel cm, 
                               int[] pixels, int offset, int scansize) {
        // start is the offset into buffer of first pixel of current source 
        // row, which will become the top pixel in destination column...
        int start = offset;
        
        // destx is zero-indexed destination column of source pixel row... 
        int destx = destwidth - y - 1;
        
        // send the pixels on to the consumer, each row of the source image
        // becomes a 1-pixel wide column of pixels in destination image...
        for (int j = 0; j < h; j++) {
            consumer.setPixels( destx, x, 1, w, cm, pixels, start, 1);
            start += scansize;
            destx--;
        }            
    }        
    public void setPixels( int x, int y, int w, int h, ColorModel cm, 
                              byte[] pixels, int offset, int scansize) {
        // start is the offset into the buffer of the first pixel of current
        // source row, which will become top pixel in destination column...
        int start = offset;
        
        // destx is zero-indexed destination column of source pixel row... 
        int destx = destwidth - y - 1;
        
        // send the pixels on to the consumer, each row of the source image
        // becomes a 1-pixel wide column of pixels in destination image...
        for (int j = 0; j < h; j++) {
            consumer.setPixels( destx, x, 1, w, cm, pixels, start, 1);
            start += scansize;
            destx--;
        }            
    }        
}



3


