home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-08-06 | 7.1 KB | 233 lines |
- import java.applet.*;
- import java.awt.*;
- import java.awt.image.*;
-
- /**
- * FunScroll Fade transition filter.
- *
- * @version @version 1.1 97/08/06
- * @author Jan Andersson (janne@torpa.se)
- */
- public class FunScrollFade
- {
- //# DONT CHANGES THESE NUMBERS, the random number generator will not
- //# work because it only produces number between 0-7
- static final int FRAMES = 7;
- static final int TOTAL_FRAMES = 8;
- static final int MULTIPLIER = 0x5D1E2F;
- int random[][] = null;
-
- FunScroll applet = null;
-
- int pixels_per_frame = 0;
- int pixels_per_image = 0;
- int image_w = 0;
- int image_h = 0;
-
- int number_of_frames = FRAMES;
- int frameIndex = 0;
- Image images[] = null;
- int work_pixels[] = null;
- int from_pixels[];
- int to_pixels[];
-
- public FunScrollFade(FunScroll applet, int width, int height)
- {
- this.applet = applet;
- image_w = width;
- image_h = height;
- pixels_per_image = image_w * image_h;
- frameIndex = 0;
- initRandomArray();
- from_pixels = null;
- to_pixels = null;
- work_pixels = null;
- images = new Image[TOTAL_FRAMES];
- applet.dbg("FunScrollFade()");
- }
-
- public void initToImage(Image image)
- {
- applet.dbg("FunScrollFade.initToImage()");
- if (to_pixels == null) {
- to_pixels = new int[pixels_per_image];
- // Create a PixelGrabber to Get the Pixels of the image and store
- // them into the to_pixels array
- PixelGrabber to_pixel_grabber = new PixelGrabber(
- image.getSource(), 0, 0,
- image_w, image_h, to_pixels, 0, image_w);
- try {
- to_pixel_grabber.grabPixels();
- } catch (InterruptedException e) {
- return;
- }
- }
- }
-
- public void initFromImage(Image image)
- {
- applet.dbg("FunScrollFade.initFromImage()");
- if (from_pixels == null) {
- from_pixels = new int[pixels_per_image];
- //# Create a PixelGrabber to Get the Pixels of the image and store
- //# them into the from_pixels array
- PixelGrabber from_pixel_grabber = new PixelGrabber(
- image.getSource(), 0, 0,
- image_w, image_h, from_pixels, 0, image_w);
- //applet.dbg("FunScrollFade created from_pixel_grabber");
- try {
- applet.dbg("FunScrollFade grabPixels()...");
- from_pixel_grabber.grabPixels();
- //applet.dbg("FunScrollFade grabPixels()...Done.");
- } catch (InterruptedException e) {
- //applet.dbg("FunScrollFade.initFromImage()\n" +
- // "InterruptedException");
- //applet.dbg(e.toString());
- return;
- }
- initWorkPixels();
- }
- applet.dbg("FunScrollFade.initFromImage().done()");
- }
-
- public Image getFromImage()
- {
- // we (might) need a copy of the from image
- Image fromImage = applet.createImage(
- new MemoryImageSource(image_w, image_h,
- from_pixels, 0, image_w));
- applet.prepareImage(fromImage, null);
- return fromImage;
- }
-
- public void resetFrameIndex()
- {
- frameIndex = 0;
- }
-
- public void drawFrame(Graphics g)
- {
- int fIndex = frameIndex;
- applet.dbg("FunScrollFade.drawFrame()");
-
- // if (frameIndex == 0)
- // initWorkPixels();
- if (frameIndex > number_of_frames)
- fIndex = number_of_frames;
-
- if (images[fIndex] == null)
- images[fIndex] = createImage(fIndex);
-
- if (images[fIndex] != null)
- g.drawImage(images[fIndex], 0, 0, null);
- frameIndex++;
- }
-
- public boolean done()
- {
- return (frameIndex > number_of_frames);
- }
-
- public void initWorkPixels()
- {
- applet.dbg("FunScrollFade.initWorkPixels()");
- work_pixels = new int[pixels_per_image];
- System.arraycopy((Object)from_pixels, 0,
- (Object)work_pixels, 0, pixels_per_image);
- }
-
- public Image createImage(int frame)
- {
- applet.dbg("FunScrollFade.createImage()");
- // draw in the pixels that the random array specifies for
- // this frame from the to pixels into the work pixels
- for(int p = 0; p < pixels_per_frame; ++p)
- work_pixels[random[frame][p]] = to_pixels[random[frame][p]];
-
- // create the new frame image from the work pixels
- Image image = applet.createImage(
- new MemoryImageSource(image_w, image_h,
- work_pixels, 0, image_w));
- applet.prepareImage(image, null);
- return image;
- }
-
- void initRandomArray() {
- pixels_per_frame = pixels_per_image / TOTAL_FRAMES;
-
- random = new int[TOTAL_FRAMES][pixels_per_frame];
-
- //# every frame will have the same number of new pixels draw into
- //# the image. No more no less. So keep track of the number
- //# of random values added to each random frame, so that we don't
- //# try to add too many. The array below keeps count
- int random_count[] = new int[TOTAL_FRAMES];
- for(int s = 0; s < TOTAL_FRAMES; ++s) {
- random_count[s] = 0;
- }
-
- int frame;
- int rounded_pixels_per_image = pixels_per_frame * TOTAL_FRAMES;
-
- //# inline random number generator starts here
- //# *** read DESIGN NOTES above ***
- int seed = (int)System.currentTimeMillis();
-
- int denominator = 10;
- while((pixels_per_frame % denominator > 0 ||
- image_h % denominator == 0) && denominator > 1) {
- --denominator;
- }
-
- int new_randoms_per_frame = pixels_per_frame / denominator;
- int new_randoms = rounded_pixels_per_image / denominator;
-
- //# create a bunch of random numbers and put them into the
- //# array without checking to see if any particular array
- //# is full. Do this until it is possible that one filled
- //# up.
- for(int p = 0; p < new_randoms_per_frame; ++p) {
- //# Generate a random number between 0 - 7
- seed *= MULTIPLIER;
- frame = (seed >>> 29);
- random[frame][random_count[frame]++] = p;
- }
-
- //# might as well as mix up the random number generator a bit more
-
- seed += 0x5050;
-
- //# generate the rest of the random numbers
- for(int p = new_randoms_per_frame; p < new_randoms; ++p) {
- //# Generate a random number between 0 - 7
- seed *= MULTIPLIER;
- frame = (seed >>> 29);
-
- //# if the frame this number is supposed to go in is
- //# full, put it in the next frame
- while(random_count[frame] >= new_randoms_per_frame) {
- if(++frame >= TOTAL_FRAMES) {
- frame = 0;
- }
- }
-
- random[frame][random_count[frame]++] = p;
- }
-
- //# we only actually filled up the arrays part of the way.
- //# now fill them up the rest of the way using the numbers
- //# we already generated. Also, we don't need to fill in
- //# the numbers for the last frame, since at the last frame
- //# we know that all the work_pixels would have been filled
- //# in with pixels from the new image anyways.
- for(int s = 0; s < FRAMES; ++s) {
- for(int ps = new_randoms_per_frame; ps < pixels_per_frame;
- ps += new_randoms_per_frame) {
- for(int p = 0; p < new_randoms_per_frame; ++p) {
- random[s][ps + p] = (random[s][p] + ps * TOTAL_FRAMES);
- }
- }
- }
- }
- }
-