/*
   FlipImage.java
   Burton Rosenberg
   Wed Jul 23 17:40:51 EDT 1997

   And example of the pixel grabbing.

*/


import java.applet.* ;
import java.awt.* ;
import java.awt.image.* ; // IMPORTANT !

public class FlipImage
extends Applet {

   Image origImg ;
   Image revImg ;

   public void init() {
      origImg = getImage( getCodeBase(), getParameter("image") ) ;

      // wait until the image is loaded ...
      MediaTracker mt = new MediaTracker(this) ;
      mt.addImage( origImg, 0 ) ;
      try {
         mt.waitForID(0) ;
      }
      catch ( InterruptedException ie ) {
         System.out.println("InterruptedException: " + ie.getMessage() ) ;
      }

      int h = origImg.getHeight(this) ;
      int w = origImg.getWidth(this) ;
      int [] pixels = new int[h*w] ;
      System.out.println("Image height= " + h) ;
      System.out.println("Image width= " + w) ;
      PixelGrabber pg = new PixelGrabber( origImg, 0, 0,
         w, h, pixels, 0, w ) ;
      // get the pixels
      try {
         pg.grabPixels() ;
      }
      catch ( InterruptedException ie ) {
         System.out.println("InterruptedException: " + ie.getMessage() ) ;
      }      

      int hw = h*w ;
      int [] slexip = new int[hw] ;
      for ( int i=0; i<hw; i++ ) slexip[hw-i-1] = pixels[i] ;

      revImg = createImage( // the image source
         (new MemoryImageSource( w, h, slexip, 0, w ))
          ) ;

   }
 
   public void paint(Graphics gc) {
      System.out.println("Drawing: " + revImg) ;
      gc.drawImage(revImg, 0, 0, this ) ;
   }
  
}

