home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / FSCROLL.EXE / SRC / FunScrollFade.java < prev   
Encoding:
Java Source  |  1997-08-06  |  7.1 KB  |  233 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.image.*;
  4.  
  5. /**
  6.  * FunScroll Fade transition filter.
  7.  *
  8.  * @version @version 1.1 97/08/06
  9.  * @author  Jan Andersson (janne@torpa.se)
  10.  */
  11. public class FunScrollFade
  12. {
  13.    //# DONT CHANGES THESE NUMBERS, the random number generator will not
  14.    //# work because it only produces number between 0-7
  15.    static final int FRAMES = 7;
  16.    static final int TOTAL_FRAMES = 8;
  17.    static final int MULTIPLIER = 0x5D1E2F;
  18.    int random[][] = null;
  19.  
  20.    FunScroll applet = null;
  21.    
  22.    int pixels_per_frame = 0;
  23.    int pixels_per_image = 0;
  24.    int image_w = 0;
  25.    int image_h = 0;
  26.  
  27.    int number_of_frames = FRAMES;
  28.    int frameIndex = 0;
  29.    Image images[] = null;
  30.    int work_pixels[] = null;
  31.    int from_pixels[];
  32.    int to_pixels[];
  33.  
  34.    public FunScrollFade(FunScroll applet, int width, int height)
  35.    {
  36.       this.applet = applet;
  37.       image_w = width;
  38.       image_h = height;
  39.       pixels_per_image = image_w * image_h;
  40.       frameIndex = 0;
  41.       initRandomArray();
  42.       from_pixels = null;
  43.       to_pixels = null;
  44.       work_pixels = null;
  45.       images = new Image[TOTAL_FRAMES];
  46.       applet.dbg("FunScrollFade()");
  47.    }
  48.  
  49.    public void initToImage(Image image)
  50.    {
  51.       applet.dbg("FunScrollFade.initToImage()");
  52.       if (to_pixels == null) {
  53.      to_pixels = new int[pixels_per_image];
  54.      // Create a PixelGrabber to Get the Pixels of the image and store
  55.      // them into the to_pixels array
  56.      PixelGrabber to_pixel_grabber = new PixelGrabber(
  57.         image.getSource(), 0, 0,
  58.         image_w, image_h, to_pixels, 0, image_w);
  59.      try {
  60.         to_pixel_grabber.grabPixels();
  61.      } catch (InterruptedException e) {
  62.         return;
  63.      }
  64.       }
  65.    }
  66.  
  67.    public void initFromImage(Image image)
  68.    {
  69.       applet.dbg("FunScrollFade.initFromImage()");
  70.       if (from_pixels == null) {
  71.      from_pixels = new int[pixels_per_image];
  72.      //# Create a PixelGrabber to Get the Pixels of the image and store
  73.      //# them into the from_pixels array
  74.      PixelGrabber from_pixel_grabber = new PixelGrabber(
  75.         image.getSource(), 0, 0,
  76.         image_w, image_h, from_pixels, 0, image_w);
  77.      //applet.dbg("FunScrollFade created from_pixel_grabber");
  78.      try {
  79.         applet.dbg("FunScrollFade grabPixels()...");
  80.         from_pixel_grabber.grabPixels();
  81.         //applet.dbg("FunScrollFade grabPixels()...Done.");
  82.      } catch (InterruptedException e) {
  83.         //applet.dbg("FunScrollFade.initFromImage()\n" +
  84.         // "InterruptedException");
  85.         //applet.dbg(e.toString());
  86.         return;
  87.      }
  88.      initWorkPixels();
  89.       }
  90.       applet.dbg("FunScrollFade.initFromImage().done()");
  91.    }
  92.  
  93.    public Image getFromImage()
  94.    {
  95.       // we (might) need a copy of the from image
  96.       Image fromImage = applet.createImage(
  97.      new MemoryImageSource(image_w, image_h,
  98.                    from_pixels, 0, image_w));
  99.       applet.prepareImage(fromImage, null);
  100.       return fromImage;
  101.    }
  102.  
  103.    public void resetFrameIndex()
  104.    {
  105.       frameIndex = 0;
  106.    }
  107.    
  108.    public void drawFrame(Graphics g)
  109.    {
  110.       int fIndex = frameIndex;
  111.       applet.dbg("FunScrollFade.drawFrame()");
  112.       
  113.       //      if (frameIndex == 0)
  114.       //     initWorkPixels();
  115.       if (frameIndex > number_of_frames)
  116.      fIndex = number_of_frames;
  117.      
  118.       if (images[fIndex] == null) 
  119.      images[fIndex] = createImage(fIndex);
  120.  
  121.       if (images[fIndex] != null)
  122.      g.drawImage(images[fIndex], 0, 0, null);
  123.       frameIndex++;
  124.    }
  125.  
  126.    public boolean done()
  127.    {
  128.       return (frameIndex > number_of_frames);
  129.    }
  130.  
  131.    public void initWorkPixels()
  132.    {
  133.       applet.dbg("FunScrollFade.initWorkPixels()");
  134.       work_pixels = new int[pixels_per_image];
  135.       System.arraycopy((Object)from_pixels, 0,
  136.                (Object)work_pixels, 0, pixels_per_image);           
  137.    }
  138.    
  139.    public Image createImage(int frame)
  140.    {
  141.       applet.dbg("FunScrollFade.createImage()");
  142.       // draw in the pixels that the random array specifies for
  143.       // this frame from the to pixels into the work pixels
  144.       for(int p = 0; p < pixels_per_frame; ++p)
  145.      work_pixels[random[frame][p]] = to_pixels[random[frame][p]];
  146.       
  147.       // create the new frame image from the work pixels
  148.       Image image = applet.createImage(
  149.      new MemoryImageSource(image_w, image_h,
  150.                    work_pixels, 0, image_w));
  151.       applet.prepareImage(image, null);
  152.       return image;
  153.    }
  154.  
  155.    void initRandomArray() {
  156.       pixels_per_frame = pixels_per_image / TOTAL_FRAMES;
  157.       
  158.       random = new int[TOTAL_FRAMES][pixels_per_frame];
  159.       
  160.       //# every frame will have the same number of new pixels draw into
  161.       //# the image.  No more no less.  So keep track of the number
  162.       //# of random values added to each random frame, so that we don't
  163.       //# try to add too many.  The array below keeps count 
  164.       int random_count[] = new int[TOTAL_FRAMES];
  165.       for(int s = 0; s < TOTAL_FRAMES; ++s) {
  166.      random_count[s] = 0;
  167.       }
  168.       
  169.       int frame;
  170.       int rounded_pixels_per_image = pixels_per_frame * TOTAL_FRAMES;
  171.       
  172.       //# inline random number generator starts here
  173.       //# *** read DESIGN NOTES above ***
  174.       int seed = (int)System.currentTimeMillis();
  175.       
  176.       int denominator = 10;
  177.       while((pixels_per_frame % denominator > 0 ||
  178.          image_h % denominator == 0) && denominator > 1) {
  179.      --denominator;
  180.       }
  181.       
  182.       int new_randoms_per_frame = pixels_per_frame / denominator;
  183.       int new_randoms = rounded_pixels_per_image / denominator;
  184.       
  185.       //# create a bunch of random numbers and put them into the
  186.       //# array without checking to see if any particular array
  187.       //# is full.   Do this until it is possible that one filled
  188.       //# up.
  189.       for(int p = 0; p < new_randoms_per_frame; ++p) {
  190.      //# Generate a random number between 0 - 7
  191.      seed *= MULTIPLIER;
  192.      frame = (seed >>> 29);
  193.      random[frame][random_count[frame]++] = p;
  194.       }
  195.       
  196.       //# might as well as mix up the random number generator a bit more          
  197.       
  198.       seed += 0x5050;
  199.       
  200.       //# generate the rest of the random numbers
  201.       for(int p = new_randoms_per_frame; p < new_randoms; ++p) {
  202.      //# Generate a random number between 0 - 7
  203.      seed *= MULTIPLIER;
  204.      frame = (seed >>> 29);
  205.      
  206.      //# if the frame this number is supposed to go in is
  207.      //# full, put it in the next frame
  208.      while(random_count[frame] >= new_randoms_per_frame) {
  209.         if(++frame >= TOTAL_FRAMES) {
  210.            frame = 0;
  211.         }
  212.      }
  213.      
  214.      random[frame][random_count[frame]++] = p;
  215.       }
  216.       
  217.       //# we only actually filled up the arrays part of the way.
  218.       //# now fill them up the rest of the way using the numbers
  219.       //# we already generated.  Also, we don't need to fill in
  220.       //# the numbers for the last frame, since at the last frame
  221.       //# we know that all the work_pixels would have been filled 
  222.       //# in with pixels from the new image anyways.
  223.       for(int s = 0; s < FRAMES; ++s) {
  224.      for(int ps = new_randoms_per_frame; ps < pixels_per_frame;
  225.          ps += new_randoms_per_frame) {
  226.         for(int p = 0; p < new_randoms_per_frame; ++p) {
  227.            random[s][ps + p] = (random[s][p] + ps * TOTAL_FRAMES);
  228.         }
  229.      }
  230.       }
  231.    }
  232. }
  233.