This chapter was all about the Graphicizer, an image-editing and conversion tool. Using the Graphicizer, you can read in image files, work on them, and save them back to disk.

The Graphicizer supports a number of menu items to load in image files, to write them back to disk, to undo the most recent change, and to quit the program. In addition, this application displays a set of buttons that function as image-handling tools to emboss, sharpen, brighten, blur, and reduce images.

There are several new technologies here, starting with the ImageIO class, which you use to read in images and write them back to disk. This class proves to be very handy for the Graphicizer, except that it only deals with BufferedImage objects instead of standard Image objects. By converting between BufferedImage and standard Image objects, however, the code is able to do what it is supposed to do. Graphicizer also uses a File dialog box to get the name of the file the user wants to open or save.

To work with the pixels in an image, Graphicizer uses two techniquesworking with a pixel grabber to actually grab all the pixels in the image, and working with a ConvolveOp object to apply a Kernel object to all the pixels without having to work pixel by pixel.

Using a PixelGrabber object, Graphicizer is able to extract every pixel from the image being worked on and store them in an array. To work with each pixel, you only have to address it in the array individually, which is how the application embosses images.

Working with each pixel by addressing it individually and extracting its red, green, and blue color values is one way of handling images, but there's another wayusing Kernel objects and ConvolveOp objects. These two objects will do the dirty work for you; the Kernel object lets you specify a matrix whose values will multiply a pixel and its neighboring pixels. And a ConvolveOp object's filter method lets you apply your kernel to each pixel automatically. The Graphicizer uses Kernel and ConvolveOp objects to sharpen, brighten, and blur images, all without a lot of programming.

It's possible to reduce an image's size by using the Image class's getScaledInstance method, and the Graphicizer lets the user reduce an image by a factor of two in both dimensions (if you prefer, you could implement a dialog box asking the user for a reduction factor instead of automatically using two).

Finally, Graphicize includes an Undo feature that's pretty easy to implement. Each time a new operation, such as sharpening or blurring, is started by the user, Graphicizer stores the current buffered image in a backup buffered image object. If the user selects the Undo menu item, Graphicizer simply takes the backup image and makes it the main image again. That provides one level of "undo" capabilityif you want to provide more, you could implement an entire stack of backup buffered images and then pop them as required by the user.

All in all, Graphicizer is a useful and fun application, providing a lot more image-handling power than most developers know exists in Java. Give it a try! (There's more such power coming up later in the book, incidentally, when an online application creates JPEG files from scratch and sends them back to web browsers.)

    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: