home *** CD-ROM | disk | FTP | other *** search
- package animationlib;
-
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.Point;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- public class GIFAnimator extends Applet implements Runnable {
- public static final String AppletName = "CorelGIFAnimator";
- public static final String MajorVersion = "1";
- public static final String MinorVersion = "0";
- private String m_stName;
- Vector images;
- Hashtable imageNames = new Hashtable(10);
- Hashtable durations;
- Hashtable sounds;
- Hashtable positions;
- static final int STARTUP_ID = 0;
- static final int BACKGROUND_ID = 1;
- static final int ANIMATION_ID = 2;
- URL startUpImageURL;
- Image startUpImage;
- URL backgroundImageURL;
- Image backgroundImage;
- Color backgroundColor;
- URL soundtrackURL;
- AudioClip soundtrack;
- URL hrefURL;
- String hrefTarget;
- int appWidth;
- int appHeight;
- URL imageSource;
- URL soundSource;
- Thread engine;
- int frameNum;
- Integer frameNumKey;
- int xPos;
- int yPos;
- public static final int defaultPause = 50;
- int globalPause = 50;
- boolean userPause = false;
- boolean repeat = true;
- Image offScrImage;
- Graphics offScrGC;
- MediaTracker tracker;
- boolean loaded = false;
- boolean error = false;
- static final String imageLabel = "image";
- static final String soundLabel = "sound";
- static final boolean debug = false;
- static final String userInstructions = "shift-click for errors, info";
-
- public void init() {
- this.images = new Vector(10);
- this.tracker = new MediaTracker(this);
- this.appWidth = ((Component)this).size().width;
- this.appHeight = ((Component)this).size().height;
-
- try {
- String var1 = this.getParameter(this.m_stName + "_IMAGESOURCE");
- this.imageSource = var1 == null ? ((Applet)this).getDocumentBase() : new URL(((Applet)this).getDocumentBase(), var1.trim() + "/");
- var1 = this.getParameter(this.m_stName + "_IMAGES");
- if (var1 != null) {
- StringTokenizer var2 = new StringTokenizer(var1.trim(), ",");
-
- while(var2.hasMoreTokens()) {
- this.dbg(this.imageSource.toString());
- URL var3 = new URL(this.imageSource, var2.nextToken());
- this.dbg(var3.toString());
- this.images.addElement(var3);
- }
- }
-
- var1 = this.getParameter(this.m_stName + "_PAUSE");
- this.globalPause = var1 != null ? Integer.parseInt(var1.trim()) : 50;
- var1 = this.getParameter(this.m_stName + "_REPEAT");
- this.repeat = var1 == null ? true : var1.trim().equalsIgnoreCase("yes") || var1.trim().equalsIgnoreCase("true");
- var1 = this.getParameter(this.m_stName + "_BACKGROUND");
- if (var1 != null) {
- this.backgroundImageURL = new URL(this.imageSource, var1.trim());
- }
-
- var1 = this.getParameter(this.m_stName + "_BACKGROUNDCOLOR");
- if (var1 != null) {
- this.backgroundColor = this.decodeColor(var1.trim());
- }
-
- var1 = this.getParameter(this.m_stName + "_STARTUP");
- if (var1 != null) {
- this.startUpImageURL = new URL(this.imageSource, var1);
- }
-
- var1 = this.getParameter(this.m_stName + "_SOUNDSOURCE");
- if (var1 != null) {
- this.soundSource = var1 == null ? this.imageSource : new URL(((Applet)this).getDocumentBase(), var1.trim() + "/");
- }
-
- var1 = this.getParameter(this.m_stName + "_SOUNDS");
- if (var1 != null) {
- this.sounds = this.parseSounds(var1.trim(), this.images);
- }
-
- var1 = this.getParameter(this.m_stName + "_PAUSES");
- if (var1 != null) {
- this.durations = this.parseDurations(var1.trim(), this.images);
- }
-
- var1 = this.getParameter(this.m_stName + "_POSITIONS");
- if (var1 != null) {
- this.positions = this.parsePositions(var1.trim(), this.images);
- }
-
- var1 = this.getParameter(this.m_stName + "_SOUNDTRACK");
- if (var1 != null) {
- this.soundtrackURL = new URL(this.soundSource, var1.trim());
- }
- } catch (MalformedURLException var4) {
- this.showParseError(var4);
- } catch (ParseException var5) {
- this.showParseError(var5);
- }
-
- this.setFrameNum(0);
- }
-
- public String getParameter(String var1) {
- return var1.toUpperCase().equals("NAME") ? "CorelGIFAnimator" : super.getParameter(var1);
- }
-
- final int setFrameNum(int var1) {
- this.frameNumKey = new Integer(this.frameNum = var1);
- return this.frameNum;
- }
-
- Image fetchImageAndWait(URL var1, int var2) throws InterruptedException {
- Image var3 = ((Applet)this).getImage(var1);
- this.tracker.addImage(var3, var2);
- this.imageNames.put(var3, var1);
- this.tracker.waitForID(var2);
- return var3;
- }
-
- boolean fetchImages(Vector var1) {
- if (var1 == null) {
- return true;
- } else {
- int var2 = var1.size();
-
- for(int var3 = 0; var3 < var2; ++var3) {
- Object var4 = var1.elementAt(var3);
- if (var4 instanceof URL) {
- URL var5 = (URL)var4;
- this.tellLoadingMsg(var5, "image");
- Image var6 = ((Applet)this).getImage(var5);
- this.tracker.addImage(var6, 2);
- var1.setElementAt(var6, var3);
- this.imageNames.put(var6, var5);
- }
- }
-
- try {
- this.tracker.waitForID(2);
- } catch (InterruptedException var7) {
- }
-
- if (this.tracker.isErrorID(2)) {
- return false;
- } else {
- return true;
- }
- }
- }
-
- Hashtable parseSounds(String var1, Vector var2) throws MalformedURLException {
- Hashtable var3 = new Hashtable();
- int var4 = 0;
- int var5 = var2.size();
-
- for(int var6 = 0; var6 < var1.length() && var4 < var5; ++var4) {
- int var7 = var1.indexOf(44, var6);
- if (var7 == -1) {
- var7 = var1.length();
- }
-
- String var8 = var1.substring(var6, var7);
- if (var8.length() != 0) {
- var3.put(new Integer(var4), new URL(this.soundSource, var8));
- }
-
- var6 = var7 + 1;
- }
-
- return var3;
- }
-
- URL fetchSounds(Hashtable var1) {
- Enumeration var2 = var1.keys();
-
- while(var2.hasMoreElements()) {
- Integer var3 = (Integer)var2.nextElement();
- Object var4 = var1.get(var3);
- if (var4 instanceof URL) {
- URL var5 = (URL)var4;
- this.tellLoadingMsg(var5, "sound");
-
- try {
- var1.put(var3, ((Applet)this).getAudioClip(var5));
- } catch (Exception var6) {
- return var5;
- }
- }
- }
-
- return null;
- }
-
- Hashtable parseDurations(String var1, Vector var2) {
- int var4 = this.globalPause;
- Hashtable var5 = new Hashtable();
- int var6 = 0;
- int var7 = var2.size();
-
- for(int var8 = 0; var8 < var1.length() && var6 < var7; ++var6) {
- int var9 = var1.indexOf(44, var8);
- if (var9 == -1) {
- var9 = var1.length();
- }
-
- if (var8 != var9) {
- String var3 = var1.substring(var8, var9).trim();
- if (var3.length() > 0) {
- var4 = Integer.parseInt(var3);
- }
- }
-
- var5.put(new Integer(var6), new Integer(var4));
- var8 = var9 + 1;
- }
-
- return var5;
- }
-
- Point parsePoint(String var1) throws ParseException {
- int var2 = var1.indexOf(32);
- if (var2 == -1) {
- throw new ParseException("Illegal position: " + var1);
- } else {
- return new Point(Integer.parseInt(var1.substring(0, var2).trim()), Integer.parseInt(var1.substring(var2 + 1).trim()));
- }
- }
-
- Hashtable parsePositions(String var1, Vector var2) throws ParseException {
- Hashtable var3 = new Hashtable();
- int var4 = 0;
- int var5 = var2.size();
-
- for(int var6 = 0; var6 < var1.length() && var4 < var5; ++var4) {
- int var7 = var1.indexOf(44, var6);
- if (var7 == -1) {
- var7 = var1.length();
- }
-
- if (var6 != var7) {
- var3.put(new Integer(var4), this.parsePoint(var1.substring(var6, var7).trim()));
- }
-
- var6 = var7 + 1;
- }
-
- return var3;
- }
-
- Color decodeColor(String var1) {
- int var2 = 0;
-
- try {
- if (var1.startsWith("0x")) {
- var2 = Integer.parseInt(var1.substring(2), 16);
- } else if (var1.startsWith("#")) {
- var2 = Integer.parseInt(var1.substring(1), 16);
- } else if (var1.startsWith("0") && var1.length() > 1) {
- var2 = Integer.parseInt(var1.substring(1), 8);
- } else {
- var2 = Integer.parseInt(var1, 10);
- }
-
- return new Color(var2);
- } catch (NumberFormatException var3) {
- return null;
- }
- }
-
- void dbg(String var1) {
- }
-
- void tellLoadingMsg(String var1, String var2) {
- ((Applet)this).showStatus("GIFAnimator: loading " + var2 + " " + var1);
- }
-
- void tellLoadingMsg(URL var1, String var2) {
- this.tellLoadingMsg(var1.toExternalForm(), var2);
- }
-
- void clearLoadingMessage() {
- ((Applet)this).showStatus("");
- }
-
- void loadError(String var1, String var2) {
- String var3 = "GIFAnimator: Couldn't load " + var2 + " " + var1;
- ((Applet)this).showStatus(var3);
- System.err.println(var3);
- this.error = true;
- ((Component)this).repaint();
- }
-
- void loadError(URL var1, String var2) {
- this.loadError(var1.toExternalForm(), var2);
- }
-
- void loadErrors(Object[] var1, String var2) {
- for(int var3 = 0; var3 < var1.length; ++var3) {
- if (var1[var3] instanceof Image) {
- URL var4 = (URL)this.imageNames.get((Image)var1[var3]);
- if (var4 != null) {
- this.loadError(var4, var2);
- }
- }
- }
-
- }
-
- void showParseError(Exception var1) {
- String var2 = "GIFAnimator: Parse error: " + var1;
- ((Applet)this).showStatus(var2);
- System.err.println(var2);
- this.error = true;
- ((Component)this).repaint();
- }
-
- void startPlaying() {
- if (this.soundtrack != null) {
- this.soundtrack.loop();
- }
-
- }
-
- void stopPlaying() {
- if (this.soundtrack != null) {
- this.soundtrack.stop();
- }
-
- }
-
- public void SetName(String var1) {
- this.m_stName = var1;
- }
-
- public void run() {
- AudioClip var1 = null;
- Thread var2 = Thread.currentThread();
- var2.setPriority(1);
- if (!this.loaded) {
- try {
- if (this.startUpImageURL != null) {
- this.tellLoadingMsg(this.startUpImageURL, "image");
- this.startUpImage = this.fetchImageAndWait(this.startUpImageURL, 0);
- if (this.tracker.isErrorID(0)) {
- this.loadError(this.startUpImageURL, "start-up image");
- }
-
- ((Component)this).repaint();
- }
-
- if (this.backgroundImageURL != null) {
- this.tellLoadingMsg(this.backgroundImageURL, "image");
- this.backgroundImage = this.fetchImageAndWait(this.backgroundImageURL, 1);
- if (this.tracker.isErrorID(1)) {
- this.loadError(this.backgroundImageURL, "background image");
- }
-
- ((Component)this).repaint();
- }
-
- if (!this.fetchImages(this.images)) {
- Object[] var4 = this.tracker.getErrorsAny();
- this.loadErrors(var4, "image");
- return;
- }
-
- if (this.soundtrackURL != null && this.soundtrack == null) {
- this.tellLoadingMsg(this.soundtrackURL, "image");
- this.soundtrack = ((Applet)this).getAudioClip(this.soundtrackURL);
- if (this.soundtrack == null) {
- this.loadError(this.soundtrackURL, "soundtrack");
- return;
- }
- }
-
- if (this.sounds != null) {
- URL var3 = this.fetchSounds(this.sounds);
- if (var3 != null) {
- this.loadError(var3, "sound");
- return;
- }
- }
-
- this.clearLoadingMessage();
- this.offScrImage = ((Component)this).createImage(this.appWidth, this.appHeight);
- this.offScrGC = this.offScrImage.getGraphics();
- this.offScrGC.setColor(Color.lightGray);
- this.loaded = true;
- this.error = false;
- } catch (Exception var12) {
- this.error = true;
- ((Throwable)var12).printStackTrace();
- }
- }
-
- if (!this.userPause) {
- if (this.repeat || this.frameNum < this.images.size()) {
- this.startPlaying();
- }
-
- try {
- if (this.images != null && this.images.size() > 1) {
- for(; this.appWidth > 0 && this.appHeight > 0 && this.engine == var2; this.setFrameNum(this.frameNum + 1)) {
- if (this.frameNum >= this.images.size()) {
- if (!this.repeat) {
- return;
- }
-
- this.setFrameNum(0);
- }
-
- Graphics var6 = ((Component)this).getGraphics();
- var6.clipRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
- this.paint(var6);
- var6.dispose();
- if (var1 != null) {
- var1.stop();
- }
-
- if (this.sounds != null) {
- var1 = (AudioClip)this.sounds.get(this.frameNumKey);
- if (var1 != null) {
- var1.play();
- }
- }
-
- try {
- Integer var7 = null;
- if (this.durations != null) {
- var7 = (Integer)this.durations.get(this.frameNumKey);
- }
-
- if (var7 == null) {
- Thread.sleep((long)this.globalPause);
- } else {
- Thread.sleep((long)var7);
- }
- } catch (InterruptedException var11) {
- }
- }
-
- }
- } finally {
- this.stopPlaying();
- }
- }
- }
-
- public void update(Graphics var1) {
- this.paint(var1);
- }
-
- public void paint(Graphics var1) {
- if (!this.error && this.loaded) {
- if (this.images != null && this.images.size() > 0 && this.tracker.checkID(2)) {
- if (this.frameNum < this.images.size()) {
- if (this.backgroundImage == null) {
- this.offScrGC.clearRect(0, 0, this.appWidth, this.appHeight);
- } else {
- this.offScrGC.drawImage(this.backgroundImage, 0, 0, this);
- }
-
- Image var2 = (Image)this.images.elementAt(this.frameNum);
- Point var3 = null;
- if (this.positions != null) {
- var3 = (Point)this.positions.get(this.frameNumKey);
- }
-
- if (var3 != null) {
- this.xPos = var3.x;
- this.yPos = var3.y;
- }
-
- if (this.backgroundColor != null) {
- this.offScrGC.setColor(this.backgroundColor);
- this.offScrGC.fillRect(0, 0, this.appWidth, this.appHeight);
- this.offScrGC.drawImage(var2, this.xPos, this.yPos, this.backgroundColor, this);
- } else {
- this.offScrGC.drawImage(var2, this.xPos, this.yPos, this);
- }
-
- var1.drawImage(this.offScrImage, 0, 0, this);
- return;
- }
-
- this.dbg("No more animation; drawing last image.");
- if (this.backgroundImage == null) {
- var1.fillRect(0, 0, this.appWidth, this.appHeight);
- } else {
- var1.drawImage(this.backgroundImage, 0, 0, this);
- }
-
- var1.drawImage((Image)this.images.lastElement(), 0, 0, this);
- }
-
- } else {
- if (this.startUpImage != null) {
- if (this.tracker.checkID(0)) {
- if (this.backgroundColor != null) {
- var1.setColor(this.backgroundColor);
- var1.fillRect(0, 0, this.appWidth, this.appHeight);
- }
-
- var1.drawImage(this.startUpImage, 0, 0, this);
- return;
- }
- } else if (this.backgroundImage != null) {
- if (this.tracker.checkID(1)) {
- var1.drawImage(this.backgroundImage, 0, 0, this);
- return;
- }
- } else {
- var1.clearRect(0, 0, this.appWidth, this.appHeight);
- }
-
- }
- }
-
- public void start() {
- if (this.engine == null) {
- this.engine = new Thread(this);
- this.engine.start();
- }
-
- ((Applet)this).showStatus(((Applet)this).getAppletInfo());
- }
-
- public void stop() {
- if (this.engine != null && this.engine.isAlive()) {
- this.engine.stop();
- }
-
- this.engine = null;
- }
-
- public boolean handleEvent(Event var1) {
- switch (var1.id) {
- case 402:
- case 403:
- case 404:
- this.dbg("Got event " + var1);
- return true;
- case 501:
- if ((var1.modifiers & 1) != 0) {
- return true;
- } else if (this.hrefURL != null) {
- return true;
- } else {
- if (this.loaded) {
- if (this.engine != null && this.engine.isAlive()) {
- if (this.userPause) {
- this.engine.resume();
- this.startPlaying();
- } else {
- this.engine.suspend();
- this.stopPlaying();
- }
-
- this.userPause = !this.userPause;
- } else {
- this.userPause = false;
- this.setFrameNum(0);
- this.engine = new Thread(this);
- this.engine.start();
- }
- }
-
- return true;
- }
- case 502:
- if (this.hrefURL != null && (var1.modifiers & 1) == 0) {
- ((Applet)this).getAppletContext().showDocument(this.hrefURL, this.hrefTarget);
- }
-
- return true;
- case 504:
- ((Applet)this).showStatus(((Applet)this).getAppletInfo() + " -- " + "shift-click for errors, info");
- return true;
- case 505:
- ((Applet)this).showStatus("");
- return true;
- default:
- return super.handleEvent(var1);
- }
- }
- }
-