home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue159 / files / copyjava.exe / lib / GIFBuilder.java < prev    next >
Encoding:
Java Source  |  1999-10-02  |  3.0 KB  |  112 lines

  1. /*
  2.  * @(#)GIFBuilder.java    1999/09/28
  3.  * 
  4.  * Written by David Griffiths, 1999. 
  5.  * 
  6.  * The parts of this software that were specifically written by David Griffiths
  7.  * are released into the Public Domain. However, you should note that some parts are
  8.  * based on other libraries and you should read and be aware of the additional 
  9.  * conditions that are specified in some of the attached files 
  10.  * (notably GIFImage.java and LZWCompressor.java). The GIF format is the copyright
  11.  * of CompuServe Incorporated.
  12.  * 
  13.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  14.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  15.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  16.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  17.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  18.  * THIS SOFTWARE OR ITS DERIVATIVES.
  19.  */
  20.  
  21. /**
  22.  * GIFBuilder is an Animator class for assembling and optimizing GIF animations.
  23.  * @version    1.0 1999/09/28
  24.  * @author     David Griffiths 
  25.  */
  26.  
  27. import java.lang.String;
  28. import java.net.MalformedURLException;
  29. import java.net.URL;
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import java.applet.Applet;
  33. import java.applet.AppletContext;
  34. import java.io.*;
  35. import java.net.*;
  36. import java.awt.image.*;
  37.  
  38.  
  39. public class GIFBuilder extends Animator {
  40.     private GIFImage myGIF;
  41.  
  42.     GIFBuilder(int Width, int Height) {
  43.         myGIF = new GIFImage(Width, Height);
  44.     }
  45.  
  46.     // Attributes
  47.  
  48.     public Animation getAnimation() {
  49.         return myGIF;
  50.     }
  51.  
  52.     public void setFPS(int newFps) {
  53.         myGIF.setFPS(newFps);
  54.     }
  55.  
  56.     public int getFPS() {
  57.         return myGIF.getFPS();
  58.     }
  59.  
  60.     private int currentGG[] = {};
  61.  
  62.     public int addImage(Image image, Component c) {
  63.         int frameCount = -1;
  64.         MediaTracker mediaTracker = new MediaTracker(c);
  65.         mediaTracker.addImage(image, 1);
  66.         try {
  67.             GIFGraphics newGraphics;
  68.  
  69.             mediaTracker.waitForID(1);
  70.  
  71.             newGraphics = new GIFGraphics(image, c);
  72.  
  73.             if (currentGG.length == 0) {
  74.                 currentGG = new int[newGraphics.pixels.length];
  75.                 for (int i = 0; i < currentGG.length; i++)
  76.                     currentGG[i] = newGraphics.pixels[i];
  77.             }
  78.             else {
  79.                 int top = newGraphics.height;
  80.                 int left = newGraphics.width;
  81.                 int height = 0;
  82.                 int width = 0;
  83.                 int i = 0;
  84.                 for (int y = 0; y < newGraphics.height; y++)
  85.                     for (int x = 0; x < newGraphics.width; x++)
  86.                         if (newGraphics.pixels[i] == currentGG[i])
  87.                             newGraphics.pixels[i++] = 0;
  88.                         else {
  89.                             if (x < left)
  90.                                 left = x;
  91.                             if (y < top)
  92.                                 top = y;
  93.                             if ((left + width) < x)
  94.                                 width = x - left;
  95.                             if ((top + height) < y)
  96.                                 height = y - top;
  97.                             currentGG[i] = newGraphics.pixels[i];
  98.                             i++;
  99.                         }
  100.                 newGraphics.cropTo(new Rectangle(left, top, width + 1, height + 1));
  101.             }
  102.  
  103.             frameCount = myGIF.addImage(newGraphics);
  104.         }
  105.         catch (Exception e) {
  106.             System.out.println("Unable to create GIFImage: " + e);
  107.             e.printStackTrace();
  108.         }
  109.         return frameCount;
  110.     }
  111. }
  112.