Source of LoadImageAndScale.java


  1: // Fig. 21.1: LoadImageAndScale.java
  2: // Load an image and display it in its original size and twice its 
  3: // original size. Load and display the same image as an ImageIcon.
  4: import java.awt.Graphics;
  5: import java.awt.Image;
  6: import javax.swing.ImageIcon;
  7: import javax.swing.JApplet;
  8: 
  9: public class LoadImageAndScale extends JApplet 
 10: {
 11:    private Image image1; // create Image object      
 12:    private ImageIcon image2; // create ImageIcon object
 13: 
 14:    // load image when applet is loaded
 15:    public void init()
 16:    {
 17:       image1 = getImage( getDocumentBase(), "redflowers.png" );
 18:       image2 = new ImageIcon( "yellowflowers.png" );
 19:    } // end method init
 20: 
 21:    // display image
 22:    public void paint( Graphics g )
 23:    {
 24:       super.paint( g );
 25: 
 26:       g.drawImage( image1, 0, 0, this ); // draw original image
 27: 
 28:       // draw image to fit the width and the height less 120 pixels
 29:       g.drawImage( image1, 0, 120, getWidth(), getHeight() - 120, this );
 30: 
 31:       // draw icon using its paintIcon method
 32:       image2.paintIcon( this, g, 180, 0 );
 33:    } // end method paint
 34: } // end class LoadImageAndScale
 35: 
 36: 
 37: /**************************************************************************
 38:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 39:  * Pearson Education, Inc. All Rights Reserved.                           *
 40:  *                                                                        *
 41:  * DISCLAIMER: The authors and publisher of this book have used their     *
 42:  * best efforts in preparing the book. These efforts include the          *
 43:  * development, research, and testing of the theories and programs        *
 44:  * to determine their effectiveness. The authors and publisher make       *
 45:  * no warranty of any kind, expressed or implied, with regard to these    *
 46:  * programs or to the documentation contained in these books. The authors *
 47:  * and publisher shall not be liable in any event for incidental or       *
 48:  * consequential damages in connection with, or arising out of, the       *
 49:  * furnishing, performance, or use of these programs.                     *
 50:  *************************************************************************/