Reducing an Image


If the user clicks the Reduce button, the image is reduced by a factor of two in each dimension, as you can see in Figure 3.8.

Figure 3.8. Reducing an image.


This one works by using the BufferedImage class's getScaledInstance method, which it inherits from the Image class. This method changes the size of an image, but it returns an Image object, so it takes a little work to get back to a BufferedImage object:

 if(event.getSource() == button5){     bufferedImageBackup = bufferedImage;     image = bufferedImage.getScaledInstance         (bufferedImage.getWidth()/2, bufferedImage.getHeight()/2, 0);     bufferedImage = new BufferedImage (         bufferedImage.getWidth()/2,         bufferedImage.getHeight()/2,         BufferedImage.TYPE_INT_BGR );     bufferedImage.createGraphics().drawImage(image, 0, 0, this ); . . . } 

After you convert from an Image object back to a BufferedImage object, you need to resize the window, subject to a certain minimum size, to correspond to the new image:

 if(event.getSource() == button5){     .     .     .     bufferedImage = new BufferedImage (         bufferedImage.getWidth()/2,         bufferedImage.getHeight()/2,         BufferedImage.TYPE_INT_BGR );     bufferedImage.createGraphics().drawImage(image, 0, 0, this );     setSize(getInsets().left + getInsets().right + Math.max(400,         bufferedImage.getWidth() + 60),         getInsets().top + getInsets().bottom +         Math.max(340, bufferedImage.getHeight() + 60));     button1.setBounds(30, getHeight() - 30, 60, 20);     button2.setBounds(100, getHeight() - 30, 60, 20);     button3.setBounds(170, getHeight() - 30, 60, 20);     button4.setBounds(240, getHeight() - 30, 60, 20);     button5.setBounds(310, getHeight() - 30, 60, 20);     repaint(); } 

That completes the Resize button's operationand that completes all the buttons. The Graphicizer tools are in place.

The last operation to take a look at isn't really an operation at allit's the Undo action that the user can select with the File menu's Undo menu item.



    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

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net