home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 24 / CDACTUAL24.iso / corel / BARISTA / ANIMATIONLIB / GIFANIMATOR.CLASS (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-24  |  12.5 KB  |  621 lines

  1. package animationlib;
  2.  
  3. import java.applet.Applet;
  4. import java.applet.AudioClip;
  5. import java.awt.Color;
  6. import java.awt.Component;
  7. import java.awt.Event;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.awt.Point;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.util.Enumeration;
  15. import java.util.Hashtable;
  16. import java.util.StringTokenizer;
  17. import java.util.Vector;
  18.  
  19. public class GIFAnimator extends Applet implements Runnable {
  20.    public static final String AppletName = "CorelGIFAnimator";
  21.    public static final String MajorVersion = "1";
  22.    public static final String MinorVersion = "0";
  23.    private String m_stName;
  24.    Vector images;
  25.    Hashtable imageNames = new Hashtable(10);
  26.    Hashtable durations;
  27.    Hashtable sounds;
  28.    Hashtable positions;
  29.    static final int STARTUP_ID = 0;
  30.    static final int BACKGROUND_ID = 1;
  31.    static final int ANIMATION_ID = 2;
  32.    URL startUpImageURL;
  33.    Image startUpImage;
  34.    URL backgroundImageURL;
  35.    Image backgroundImage;
  36.    Color backgroundColor;
  37.    URL soundtrackURL;
  38.    AudioClip soundtrack;
  39.    URL hrefURL;
  40.    String hrefTarget;
  41.    int appWidth;
  42.    int appHeight;
  43.    URL imageSource;
  44.    URL soundSource;
  45.    Thread engine;
  46.    int frameNum;
  47.    Integer frameNumKey;
  48.    int xPos;
  49.    int yPos;
  50.    public static final int defaultPause = 50;
  51.    int globalPause = 50;
  52.    boolean userPause = false;
  53.    boolean repeat = true;
  54.    Image offScrImage;
  55.    Graphics offScrGC;
  56.    MediaTracker tracker;
  57.    boolean loaded = false;
  58.    boolean error = false;
  59.    static final String imageLabel = "image";
  60.    static final String soundLabel = "sound";
  61.    static final boolean debug = false;
  62.    static final String userInstructions = "shift-click for errors, info";
  63.  
  64.    public void init() {
  65.       this.images = new Vector(10);
  66.       this.tracker = new MediaTracker(this);
  67.       this.appWidth = ((Component)this).size().width;
  68.       this.appHeight = ((Component)this).size().height;
  69.  
  70.       try {
  71.          String var1 = this.getParameter(this.m_stName + "_IMAGESOURCE");
  72.          this.imageSource = var1 == null ? ((Applet)this).getDocumentBase() : new URL(((Applet)this).getDocumentBase(), var1.trim() + "/");
  73.          var1 = this.getParameter(this.m_stName + "_IMAGES");
  74.          if (var1 != null) {
  75.             StringTokenizer var2 = new StringTokenizer(var1.trim(), ",");
  76.  
  77.             while(var2.hasMoreTokens()) {
  78.                this.dbg(this.imageSource.toString());
  79.                URL var3 = new URL(this.imageSource, var2.nextToken());
  80.                this.dbg(var3.toString());
  81.                this.images.addElement(var3);
  82.             }
  83.          }
  84.  
  85.          var1 = this.getParameter(this.m_stName + "_PAUSE");
  86.          this.globalPause = var1 != null ? Integer.parseInt(var1.trim()) : 50;
  87.          var1 = this.getParameter(this.m_stName + "_REPEAT");
  88.          this.repeat = var1 == null ? true : var1.trim().equalsIgnoreCase("yes") || var1.trim().equalsIgnoreCase("true");
  89.          var1 = this.getParameter(this.m_stName + "_BACKGROUND");
  90.          if (var1 != null) {
  91.             this.backgroundImageURL = new URL(this.imageSource, var1.trim());
  92.          }
  93.  
  94.          var1 = this.getParameter(this.m_stName + "_BACKGROUNDCOLOR");
  95.          if (var1 != null) {
  96.             this.backgroundColor = this.decodeColor(var1.trim());
  97.          }
  98.  
  99.          var1 = this.getParameter(this.m_stName + "_STARTUP");
  100.          if (var1 != null) {
  101.             this.startUpImageURL = new URL(this.imageSource, var1);
  102.          }
  103.  
  104.          var1 = this.getParameter(this.m_stName + "_SOUNDSOURCE");
  105.          if (var1 != null) {
  106.             this.soundSource = var1 == null ? this.imageSource : new URL(((Applet)this).getDocumentBase(), var1.trim() + "/");
  107.          }
  108.  
  109.          var1 = this.getParameter(this.m_stName + "_SOUNDS");
  110.          if (var1 != null) {
  111.             this.sounds = this.parseSounds(var1.trim(), this.images);
  112.          }
  113.  
  114.          var1 = this.getParameter(this.m_stName + "_PAUSES");
  115.          if (var1 != null) {
  116.             this.durations = this.parseDurations(var1.trim(), this.images);
  117.          }
  118.  
  119.          var1 = this.getParameter(this.m_stName + "_POSITIONS");
  120.          if (var1 != null) {
  121.             this.positions = this.parsePositions(var1.trim(), this.images);
  122.          }
  123.  
  124.          var1 = this.getParameter(this.m_stName + "_SOUNDTRACK");
  125.          if (var1 != null) {
  126.             this.soundtrackURL = new URL(this.soundSource, var1.trim());
  127.          }
  128.       } catch (MalformedURLException var4) {
  129.          this.showParseError(var4);
  130.       } catch (ParseException var5) {
  131.          this.showParseError(var5);
  132.       }
  133.  
  134.       this.setFrameNum(0);
  135.    }
  136.  
  137.    public String getParameter(String var1) {
  138.       return var1.toUpperCase().equals("NAME") ? "CorelGIFAnimator" : super.getParameter(var1);
  139.    }
  140.  
  141.    final int setFrameNum(int var1) {
  142.       this.frameNumKey = new Integer(this.frameNum = var1);
  143.       return this.frameNum;
  144.    }
  145.  
  146.    Image fetchImageAndWait(URL var1, int var2) throws InterruptedException {
  147.       Image var3 = ((Applet)this).getImage(var1);
  148.       this.tracker.addImage(var3, var2);
  149.       this.imageNames.put(var3, var1);
  150.       this.tracker.waitForID(var2);
  151.       return var3;
  152.    }
  153.  
  154.    boolean fetchImages(Vector var1) {
  155.       if (var1 == null) {
  156.          return true;
  157.       } else {
  158.          int var2 = var1.size();
  159.  
  160.          for(int var3 = 0; var3 < var2; ++var3) {
  161.             Object var4 = var1.elementAt(var3);
  162.             if (var4 instanceof URL) {
  163.                URL var5 = (URL)var4;
  164.                this.tellLoadingMsg(var5, "image");
  165.                Image var6 = ((Applet)this).getImage(var5);
  166.                this.tracker.addImage(var6, 2);
  167.                var1.setElementAt(var6, var3);
  168.                this.imageNames.put(var6, var5);
  169.             }
  170.          }
  171.  
  172.          try {
  173.             this.tracker.waitForID(2);
  174.          } catch (InterruptedException var7) {
  175.          }
  176.  
  177.          if (this.tracker.isErrorID(2)) {
  178.             return false;
  179.          } else {
  180.             return true;
  181.          }
  182.       }
  183.    }
  184.  
  185.    Hashtable parseSounds(String var1, Vector var2) throws MalformedURLException {
  186.       Hashtable var3 = new Hashtable();
  187.       int var4 = 0;
  188.       int var5 = var2.size();
  189.  
  190.       for(int var6 = 0; var6 < var1.length() && var4 < var5; ++var4) {
  191.          int var7 = var1.indexOf(44, var6);
  192.          if (var7 == -1) {
  193.             var7 = var1.length();
  194.          }
  195.  
  196.          String var8 = var1.substring(var6, var7);
  197.          if (var8.length() != 0) {
  198.             var3.put(new Integer(var4), new URL(this.soundSource, var8));
  199.          }
  200.  
  201.          var6 = var7 + 1;
  202.       }
  203.  
  204.       return var3;
  205.    }
  206.  
  207.    URL fetchSounds(Hashtable var1) {
  208.       Enumeration var2 = var1.keys();
  209.  
  210.       while(var2.hasMoreElements()) {
  211.          Integer var3 = (Integer)var2.nextElement();
  212.          Object var4 = var1.get(var3);
  213.          if (var4 instanceof URL) {
  214.             URL var5 = (URL)var4;
  215.             this.tellLoadingMsg(var5, "sound");
  216.  
  217.             try {
  218.                var1.put(var3, ((Applet)this).getAudioClip(var5));
  219.             } catch (Exception var6) {
  220.                return var5;
  221.             }
  222.          }
  223.       }
  224.  
  225.       return null;
  226.    }
  227.  
  228.    Hashtable parseDurations(String var1, Vector var2) {
  229.       int var4 = this.globalPause;
  230.       Hashtable var5 = new Hashtable();
  231.       int var6 = 0;
  232.       int var7 = var2.size();
  233.  
  234.       for(int var8 = 0; var8 < var1.length() && var6 < var7; ++var6) {
  235.          int var9 = var1.indexOf(44, var8);
  236.          if (var9 == -1) {
  237.             var9 = var1.length();
  238.          }
  239.  
  240.          if (var8 != var9) {
  241.             String var3 = var1.substring(var8, var9).trim();
  242.             if (var3.length() > 0) {
  243.                var4 = Integer.parseInt(var3);
  244.             }
  245.          }
  246.  
  247.          var5.put(new Integer(var6), new Integer(var4));
  248.          var8 = var9 + 1;
  249.       }
  250.  
  251.       return var5;
  252.    }
  253.  
  254.    Point parsePoint(String var1) throws ParseException {
  255.       int var2 = var1.indexOf(32);
  256.       if (var2 == -1) {
  257.          throw new ParseException("Illegal position: " + var1);
  258.       } else {
  259.          return new Point(Integer.parseInt(var1.substring(0, var2).trim()), Integer.parseInt(var1.substring(var2 + 1).trim()));
  260.       }
  261.    }
  262.  
  263.    Hashtable parsePositions(String var1, Vector var2) throws ParseException {
  264.       Hashtable var3 = new Hashtable();
  265.       int var4 = 0;
  266.       int var5 = var2.size();
  267.  
  268.       for(int var6 = 0; var6 < var1.length() && var4 < var5; ++var4) {
  269.          int var7 = var1.indexOf(44, var6);
  270.          if (var7 == -1) {
  271.             var7 = var1.length();
  272.          }
  273.  
  274.          if (var6 != var7) {
  275.             var3.put(new Integer(var4), this.parsePoint(var1.substring(var6, var7).trim()));
  276.          }
  277.  
  278.          var6 = var7 + 1;
  279.       }
  280.  
  281.       return var3;
  282.    }
  283.  
  284.    Color decodeColor(String var1) {
  285.       int var2 = 0;
  286.  
  287.       try {
  288.          if (var1.startsWith("0x")) {
  289.             var2 = Integer.parseInt(var1.substring(2), 16);
  290.          } else if (var1.startsWith("#")) {
  291.             var2 = Integer.parseInt(var1.substring(1), 16);
  292.          } else if (var1.startsWith("0") && var1.length() > 1) {
  293.             var2 = Integer.parseInt(var1.substring(1), 8);
  294.          } else {
  295.             var2 = Integer.parseInt(var1, 10);
  296.          }
  297.  
  298.          return new Color(var2);
  299.       } catch (NumberFormatException var3) {
  300.          return null;
  301.       }
  302.    }
  303.  
  304.    void dbg(String var1) {
  305.    }
  306.  
  307.    void tellLoadingMsg(String var1, String var2) {
  308.       ((Applet)this).showStatus("GIFAnimator: loading " + var2 + " " + var1);
  309.    }
  310.  
  311.    void tellLoadingMsg(URL var1, String var2) {
  312.       this.tellLoadingMsg(var1.toExternalForm(), var2);
  313.    }
  314.  
  315.    void clearLoadingMessage() {
  316.       ((Applet)this).showStatus("");
  317.    }
  318.  
  319.    void loadError(String var1, String var2) {
  320.       String var3 = "GIFAnimator: Couldn't load " + var2 + " " + var1;
  321.       ((Applet)this).showStatus(var3);
  322.       System.err.println(var3);
  323.       this.error = true;
  324.       ((Component)this).repaint();
  325.    }
  326.  
  327.    void loadError(URL var1, String var2) {
  328.       this.loadError(var1.toExternalForm(), var2);
  329.    }
  330.  
  331.    void loadErrors(Object[] var1, String var2) {
  332.       for(int var3 = 0; var3 < var1.length; ++var3) {
  333.          if (var1[var3] instanceof Image) {
  334.             URL var4 = (URL)this.imageNames.get((Image)var1[var3]);
  335.             if (var4 != null) {
  336.                this.loadError(var4, var2);
  337.             }
  338.          }
  339.       }
  340.  
  341.    }
  342.  
  343.    void showParseError(Exception var1) {
  344.       String var2 = "GIFAnimator: Parse error: " + var1;
  345.       ((Applet)this).showStatus(var2);
  346.       System.err.println(var2);
  347.       this.error = true;
  348.       ((Component)this).repaint();
  349.    }
  350.  
  351.    void startPlaying() {
  352.       if (this.soundtrack != null) {
  353.          this.soundtrack.loop();
  354.       }
  355.  
  356.    }
  357.  
  358.    void stopPlaying() {
  359.       if (this.soundtrack != null) {
  360.          this.soundtrack.stop();
  361.       }
  362.  
  363.    }
  364.  
  365.    public void SetName(String var1) {
  366.       this.m_stName = var1;
  367.    }
  368.  
  369.    public void run() {
  370.       AudioClip var1 = null;
  371.       Thread var2 = Thread.currentThread();
  372.       var2.setPriority(1);
  373.       if (!this.loaded) {
  374.          try {
  375.             if (this.startUpImageURL != null) {
  376.                this.tellLoadingMsg(this.startUpImageURL, "image");
  377.                this.startUpImage = this.fetchImageAndWait(this.startUpImageURL, 0);
  378.                if (this.tracker.isErrorID(0)) {
  379.                   this.loadError(this.startUpImageURL, "start-up image");
  380.                }
  381.  
  382.                ((Component)this).repaint();
  383.             }
  384.  
  385.             if (this.backgroundImageURL != null) {
  386.                this.tellLoadingMsg(this.backgroundImageURL, "image");
  387.                this.backgroundImage = this.fetchImageAndWait(this.backgroundImageURL, 1);
  388.                if (this.tracker.isErrorID(1)) {
  389.                   this.loadError(this.backgroundImageURL, "background image");
  390.                }
  391.  
  392.                ((Component)this).repaint();
  393.             }
  394.  
  395.             if (!this.fetchImages(this.images)) {
  396.                Object[] var4 = this.tracker.getErrorsAny();
  397.                this.loadErrors(var4, "image");
  398.                return;
  399.             }
  400.  
  401.             if (this.soundtrackURL != null && this.soundtrack == null) {
  402.                this.tellLoadingMsg(this.soundtrackURL, "image");
  403.                this.soundtrack = ((Applet)this).getAudioClip(this.soundtrackURL);
  404.                if (this.soundtrack == null) {
  405.                   this.loadError(this.soundtrackURL, "soundtrack");
  406.                   return;
  407.                }
  408.             }
  409.  
  410.             if (this.sounds != null) {
  411.                URL var3 = this.fetchSounds(this.sounds);
  412.                if (var3 != null) {
  413.                   this.loadError(var3, "sound");
  414.                   return;
  415.                }
  416.             }
  417.  
  418.             this.clearLoadingMessage();
  419.             this.offScrImage = ((Component)this).createImage(this.appWidth, this.appHeight);
  420.             this.offScrGC = this.offScrImage.getGraphics();
  421.             this.offScrGC.setColor(Color.lightGray);
  422.             this.loaded = true;
  423.             this.error = false;
  424.          } catch (Exception var12) {
  425.             this.error = true;
  426.             ((Throwable)var12).printStackTrace();
  427.          }
  428.       }
  429.  
  430.       if (!this.userPause) {
  431.          if (this.repeat || this.frameNum < this.images.size()) {
  432.             this.startPlaying();
  433.          }
  434.  
  435.          try {
  436.             if (this.images != null && this.images.size() > 1) {
  437.                for(; this.appWidth > 0 && this.appHeight > 0 && this.engine == var2; this.setFrameNum(this.frameNum + 1)) {
  438.                   if (this.frameNum >= this.images.size()) {
  439.                      if (!this.repeat) {
  440.                         return;
  441.                      }
  442.  
  443.                      this.setFrameNum(0);
  444.                   }
  445.  
  446.                   Graphics var6 = ((Component)this).getGraphics();
  447.                   var6.clipRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  448.                   this.paint(var6);
  449.                   var6.dispose();
  450.                   if (var1 != null) {
  451.                      var1.stop();
  452.                   }
  453.  
  454.                   if (this.sounds != null) {
  455.                      var1 = (AudioClip)this.sounds.get(this.frameNumKey);
  456.                      if (var1 != null) {
  457.                         var1.play();
  458.                      }
  459.                   }
  460.  
  461.                   try {
  462.                      Integer var7 = null;
  463.                      if (this.durations != null) {
  464.                         var7 = (Integer)this.durations.get(this.frameNumKey);
  465.                      }
  466.  
  467.                      if (var7 == null) {
  468.                         Thread.sleep((long)this.globalPause);
  469.                      } else {
  470.                         Thread.sleep((long)var7);
  471.                      }
  472.                   } catch (InterruptedException var11) {
  473.                   }
  474.                }
  475.  
  476.             }
  477.          } finally {
  478.             this.stopPlaying();
  479.          }
  480.       }
  481.    }
  482.  
  483.    public void update(Graphics var1) {
  484.       this.paint(var1);
  485.    }
  486.  
  487.    public void paint(Graphics var1) {
  488.       if (!this.error && this.loaded) {
  489.          if (this.images != null && this.images.size() > 0 && this.tracker.checkID(2)) {
  490.             if (this.frameNum < this.images.size()) {
  491.                if (this.backgroundImage == null) {
  492.                   this.offScrGC.clearRect(0, 0, this.appWidth, this.appHeight);
  493.                } else {
  494.                   this.offScrGC.drawImage(this.backgroundImage, 0, 0, this);
  495.                }
  496.  
  497.                Image var2 = (Image)this.images.elementAt(this.frameNum);
  498.                Point var3 = null;
  499.                if (this.positions != null) {
  500.                   var3 = (Point)this.positions.get(this.frameNumKey);
  501.                }
  502.  
  503.                if (var3 != null) {
  504.                   this.xPos = var3.x;
  505.                   this.yPos = var3.y;
  506.                }
  507.  
  508.                if (this.backgroundColor != null) {
  509.                   this.offScrGC.setColor(this.backgroundColor);
  510.                   this.offScrGC.fillRect(0, 0, this.appWidth, this.appHeight);
  511.                   this.offScrGC.drawImage(var2, this.xPos, this.yPos, this.backgroundColor, this);
  512.                } else {
  513.                   this.offScrGC.drawImage(var2, this.xPos, this.yPos, this);
  514.                }
  515.  
  516.                var1.drawImage(this.offScrImage, 0, 0, this);
  517.                return;
  518.             }
  519.  
  520.             this.dbg("No more animation; drawing last image.");
  521.             if (this.backgroundImage == null) {
  522.                var1.fillRect(0, 0, this.appWidth, this.appHeight);
  523.             } else {
  524.                var1.drawImage(this.backgroundImage, 0, 0, this);
  525.             }
  526.  
  527.             var1.drawImage((Image)this.images.lastElement(), 0, 0, this);
  528.          }
  529.  
  530.       } else {
  531.          if (this.startUpImage != null) {
  532.             if (this.tracker.checkID(0)) {
  533.                if (this.backgroundColor != null) {
  534.                   var1.setColor(this.backgroundColor);
  535.                   var1.fillRect(0, 0, this.appWidth, this.appHeight);
  536.                }
  537.  
  538.                var1.drawImage(this.startUpImage, 0, 0, this);
  539.                return;
  540.             }
  541.          } else if (this.backgroundImage != null) {
  542.             if (this.tracker.checkID(1)) {
  543.                var1.drawImage(this.backgroundImage, 0, 0, this);
  544.                return;
  545.             }
  546.          } else {
  547.             var1.clearRect(0, 0, this.appWidth, this.appHeight);
  548.          }
  549.  
  550.       }
  551.    }
  552.  
  553.    public void start() {
  554.       if (this.engine == null) {
  555.          this.engine = new Thread(this);
  556.          this.engine.start();
  557.       }
  558.  
  559.       ((Applet)this).showStatus(((Applet)this).getAppletInfo());
  560.    }
  561.  
  562.    public void stop() {
  563.       if (this.engine != null && this.engine.isAlive()) {
  564.          this.engine.stop();
  565.       }
  566.  
  567.       this.engine = null;
  568.    }
  569.  
  570.    public boolean handleEvent(Event var1) {
  571.       switch (var1.id) {
  572.          case 402:
  573.          case 403:
  574.          case 404:
  575.             this.dbg("Got event " + var1);
  576.             return true;
  577.          case 501:
  578.             if ((var1.modifiers & 1) != 0) {
  579.                return true;
  580.             } else if (this.hrefURL != null) {
  581.                return true;
  582.             } else {
  583.                if (this.loaded) {
  584.                   if (this.engine != null && this.engine.isAlive()) {
  585.                      if (this.userPause) {
  586.                         this.engine.resume();
  587.                         this.startPlaying();
  588.                      } else {
  589.                         this.engine.suspend();
  590.                         this.stopPlaying();
  591.                      }
  592.  
  593.                      this.userPause = !this.userPause;
  594.                   } else {
  595.                      this.userPause = false;
  596.                      this.setFrameNum(0);
  597.                      this.engine = new Thread(this);
  598.                      this.engine.start();
  599.                   }
  600.                }
  601.  
  602.                return true;
  603.             }
  604.          case 502:
  605.             if (this.hrefURL != null && (var1.modifiers & 1) == 0) {
  606.                ((Applet)this).getAppletContext().showDocument(this.hrefURL, this.hrefTarget);
  607.             }
  608.  
  609.             return true;
  610.          case 504:
  611.             ((Applet)this).showStatus(((Applet)this).getAppletInfo() + " -- " + "shift-click for errors, info");
  612.             return true;
  613.          case 505:
  614.             ((Applet)this).showStatus("");
  615.             return true;
  616.          default:
  617.             return super.handleEvent(var1);
  618.       }
  619.    }
  620. }
  621.