// // another example of the Java AWT, showing loading and display of // an image. // see class definition for details. // // this example is modified from a similar example in chapter 17 of // _Exploring Java_ (Niemeyer and Peck). // import java.awt.* ; import java.awt.image.* ; import java.io.* ; // // a ColorSep object is a DrawingFrame that obtains an image from a // file (GIF and JPEG formats are supported), and then displays // four versions of it, the original and three colored-filtered // versions. // class ColorSep extends DrawingFrame { private Image img ; private Image redImg, greenImg, blueImg ; private int imgWidth, imgHeight ; ColorSep(String imageFile) { super("ColorSep") ; // start loading image. img = getToolkit().getImage(imageFile) ; System.out.println("loading image ....") ; // wait for completion. waitForImage(img) ; System.out.println("done") ; // get size of loaded image and use to set // initial size (big enough for four copies). imgWidth = img.getWidth(this) ; imgHeight = img.getHeight(this) ; setDrawingSize(new Dimension(2*imgWidth, 2*imgHeight)) ; // create filtered images 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))) ; show() ; } // waits for image to finish loading. private void waitForImage(Image im) { MediaTracker tracker = new MediaTracker(this) ; // add desired image to list of things the // MediaTracker is tracking, with id = 0. tracker.addImage(im, 0) ; try { // wait for loading of image we just // added to MediaTracker's list. tracker.waitForID(0) ; } catch(InterruptedException e) { } } // creates onscreen image by scaling image from file. public void paint(Graphics g) { System.out.println("paint called") ; // position origin to avoid frame borders. translateDrawingArea(g) ; // draw original image and three filtered versions. g.drawImage(img, 0, 0, this) ; g.drawImage(redImg, imgWidth, 0, this) ; g.drawImage(greenImg, 0, imgHeight, this) ; g.drawImage(blueImg, imgWidth, imgHeight, this) ; } // updates onscreen image (no need to clear first, just paint). public void update(Graphics g) { System.out.println("update called") ; paint(g) ; } // // ---- main --------------------------------------------------- // // command-line argument: file from which to load image. // public static void main(String[] args) { if (args.length < 1) { System.out.println("Argument is filename") ; System.exit(-1) ; } File f = new File(args[0]) ; if (!f.exists() || !f.canRead()) { System.out.println("Filename not found") ; System.exit(-1) ; } ColorSep s = new ColorSep(args[0]) ; } } // // a ColorMaskFilter object filters pixel data, multiplying its // red, green, and blue components by the corresponding // components of the mask. // class ColorMaskFilter extends RGBImageFilter { private Color msk ; ColorMaskFilter(Color mask) { msk = mask ; canFilterIndexColorModel = true ; } public int filterRGB(int x, int y, int pixel) { return (255 << 24) | ((redPart(pixel) * msk.getRed()/255) << 16) | ((greenPart(pixel) * msk.getGreen()/255) << 8) | ((bluePart(pixel) * msk.getBlue()/255)) ; } private int redPart(int pixel) { return (pixel & 0xff0000) >> 16 ; } private int greenPart(int pixel) { return (pixel & 0xff00) >> 8 ; } private int bluePart(int pixel) { return (pixel & 0xff) ; } }