import java.awt.*;
import java.awt.image.*;
public class ColorSep extends java.applet.Applet {
Image img, redImg, greenImg, blueImg;
public void init() {
img = getImage( getClass().getResource( getParameter("img")) );
redImg = createImage(new FilteredImageSource(img.getSource(),
new ColorMaskFilter( Color.red )));
greenImg = createImage(new FilteredImageSource(img.getSource(),
new ColorMaskFilter( Color.green )));
blueImg = createImage(new FilteredImageSource(img.getSource(),
new ColorMaskFilter( Color.blue )));
}
public void paint( Graphics g ) {
int width = getSize().width, height = getSize().height;
g.drawImage( redImg, 0, 0, width/3, height, this );
g.drawImage( greenImg, width/3, 0, width/3, height, this );
g.drawImage( blueImg, 2*width/3, 0, width/3, height, this );
}
}
class ColorMaskFilter extends RGBImageFilter {
Color color;
ColorMaskFilter( Color mask ) {
color = mask;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int pixel ) {
return
255 << 24 |
(((pixel & 0xff0000) >> 16) * color.getRed()/255) << 16 |
(((pixel & 0xff00) >> 8) * color.getGreen()/255) << 8 |
(pixel & 0xff) * color.getBlue()/255 ;
}
}
|