home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.Point;
- import java.awt.image.ImageObserver;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
-
- public class Animator extends Applet implements Runnable {
- 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 = 3900;
- int globalPause = 3900;
- boolean userPause = false;
- boolean repeat;
- 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 sourceLocation = "http://java.sun.com/applets/applets/Animator/";
- static final String userInstructions = "shift-click for errors, info";
-
- public String getAppletInfo() {
- return "Animator v1.9 (06/18/96), by Herb Jellinek";
- }
-
- public String[][] getParameterInfo() {
- String[][] var1 = new String[][]{{"imagesource", "URL", "a directory"}, {"startup", "URL", "image displayed at start-up"}, {"backgroundcolor", "int", "color (24-bit RGB number) displayed as background"}, {"background", "URL", "image displayed as background"}, {"startimage", "int", "index of first image"}, {"endimage", "int", "index of last image"}, {"namepattern", "URL", "generates indexed names"}, {"images", "URLs", "list of image indices"}, {"href", "URL", "page to visit on mouse-click"}, {"target", "name", "frame to put that page in"}, {"pause", "int", "global pause, milliseconds"}, {"pauses", "ints", "individual pauses, milliseconds"}, {"repeat", "boolean", "repeat? true or false"}, {"positions", "coordinates", "path images will follow"}, {"soundsource", "URL", "audio directory"}, {"soundtrack", "URL", "background music"}, {"sounds", "URLs", "list of audio samples"}};
- return var1;
- }
-
- void showDescription() {
- DescriptionFrame var1 = new DescriptionFrame();
- var1.tell("\t\t" + this.getAppletInfo() + "\n");
- var1.tell("Updates, documentation at http://java.sun.com/applets/applets/Animator/\n\n");
- var1.tell("Document base: " + ((Applet)this).getDocumentBase() + "\n");
- var1.tell("Code base: " + ((Applet)this).getCodeBase() + "\n\n");
- Object[] var2 = this.tracker.getErrorsAny();
- if (var2 != null) {
- var1.tell("Applet image errors:\n");
-
- for(int var3 = 0; var3 < var2.length; ++var3) {
- if (var2[var3] instanceof Image) {
- URL var4 = (URL)this.imageNames.get((Image)var2[var3]);
- if (var4 != null) {
- var1.tell(" " + var4 + " not loaded\n");
- }
- }
- }
-
- var1.tell("\n");
- }
-
- if (this.images == null || this.images.size() == 0) {
- var1.tell("\n** No images loaded **\n\n");
- }
-
- var1.tell("Applet parameters:\n");
- var1.tell(" width = " + ((Applet)this).getParameter("WIDTH") + "\n");
- var1.tell(" height = " + ((Applet)this).getParameter("HEIGHT") + "\n");
- String[][] var6 = this.getParameterInfo();
-
- for(int var7 = 0; var7 < var6.length; ++var7) {
- String var5 = var6[var7][0];
- var1.tell(" " + var5 + " = " + ((Applet)this).getParameter(var5) + "\t [" + var6[var7][2] + "]\n");
- }
-
- var1.show();
- }
-
- void dbg(String var1) {
- }
-
- public String getParam(String var1) {
- String var2 = ((Applet)this).getParameter(var1);
- this.dbg("getParameter(" + var1 + ") = " + var2);
- return var2;
- }
-
- final int setFrameNum(int var1) {
- this.frameNumKey = new Integer(this.frameNum = var1);
- return this.frameNum;
- }
-
- Vector parseImages(String var1, String var2) throws MalformedURLException {
- if (var2 == null) {
- var2 = "T%N.gif";
- }
-
- Vector var3 = new Vector(10);
-
- int var5;
- for(int var4 = 0; var4 < var1.length(); var4 = var5 + 1) {
- var5 = var1.indexOf(124, var4);
- if (var5 == -1) {
- var5 = var1.length();
- }
-
- String var6 = var1.substring(var4, var5);
- var3.addElement(new URL(this.imageSource, this.doSubst(var2, var6)));
- }
-
- return var3;
- }
-
- 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(124, 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) {
- 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(124, var6);
- if (var7 == -1) {
- var7 = var1.length();
- }
-
- if (var6 != var7) {
- int var8 = Integer.parseInt(var1.substring(var6, var7));
- var3.put(new Integer(var4), new Integer(var8));
- } else {
- var3.put(new Integer(var4), new Integer(this.globalPause));
- }
-
- var6 = var7 + 1;
- }
-
- return var3;
- }
-
- Point parsePoint(String var1) throws ParseException {
- int var2 = var1.indexOf(64);
- if (var2 == -1) {
- throw new ParseException("Illegal position: " + var1);
- } else {
- return new Point(Integer.parseInt(var1.substring(0, var2)), Integer.parseInt(var1.substring(var2 + 1)));
- }
- }
-
- 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(124, var6);
- if (var7 == -1) {
- var7 = var1.length();
- }
-
- if (var6 != var7) {
- var3.put(new Integer(var4), this.parsePoint(var1.substring(var6, var7)));
- }
-
- var6 = var7 + 1;
- }
-
- return var3;
- }
-
- Dimension getImageDimensions(Image var1) {
- return new Dimension(var1.getWidth((ImageObserver)null), var1.getHeight((ImageObserver)null));
- }
-
- String doSubst(String var1, String var2) {
- String var3 = "0000000000";
- int var4 = var1.length();
- StringBuffer var5 = new StringBuffer(var4);
- int var6 = 0;
-
- while(var6 < var4) {
- char var7 = var1.charAt(var6);
- if (var7 == '%') {
- ++var6;
- if (var6 == var4) {
- var5.append(var7);
- } else {
- var7 = var1.charAt(var6);
- if (var7 != 'N' && var7 != 'n') {
- int var8;
- if ((var8 = Character.digit(var7, 10)) != -1) {
- String var10 = var3 + var2;
- var5.append(var10.substring(var10.length() - var8));
- ++var6;
- } else {
- var5.append(var7);
- ++var6;
- }
- } else {
- var5.append(var2);
- ++var6;
- }
- }
- } else {
- var5.append(var7);
- ++var6;
- }
- }
-
- return var5.toString();
- }
-
- Vector prepareImageRange(int var1, int var2, String var3) throws MalformedURLException {
- Vector var4 = new Vector(Math.abs(var2 - var1) + 1);
- if (var3 == null) {
- var3 = "T%N.gif";
- }
-
- if (var1 > var2) {
- for(int var5 = var1; var5 >= var2; --var5) {
- var4.addElement(new URL(this.imageSource, this.doSubst(var3, var5 + "")));
- }
- } else {
- for(int var6 = var1; var6 <= var2; ++var6) {
- var4.addElement(new URL(this.imageSource, this.doSubst(var3, var6 + "")));
- }
- }
-
- return var4;
- }
-
- public void init() {
- this.tracker = new MediaTracker(this);
- this.appWidth = ((Component)this).size().width;
- this.appHeight = ((Component)this).size().height;
-
- try {
- String var1 = this.getParam("IMAGESOURCE");
- this.imageSource = var1 == null ? ((Applet)this).getDocumentBase() : new URL(((Applet)this).getDocumentBase(), var1 + "/");
- String var2 = this.getParam("HREF");
- if (var2 != null) {
- try {
- this.hrefURL = new URL(((Applet)this).getDocumentBase(), var2);
- } catch (MalformedURLException var5) {
- this.showParseError(var5);
- }
- }
-
- this.hrefTarget = this.getParam("TARGET");
- if (this.hrefTarget == null) {
- this.hrefTarget = "_top";
- }
-
- var1 = this.getParam("PAUSE");
- this.globalPause = var1 != null ? Integer.parseInt(var1) : 3900;
- var1 = this.getParam("REPEAT");
- this.repeat = var1 == null ? true : var1.equalsIgnoreCase("yes") || var1.equalsIgnoreCase("true");
- int var3 = 1;
- int var4 = 1;
- var1 = this.getParam("ENDIMAGE");
- if (var1 != null) {
- var4 = Integer.parseInt(var1);
- var1 = this.getParam("STARTIMAGE");
- if (var1 != null) {
- var3 = Integer.parseInt(var1);
- }
-
- var1 = this.getParam("NAMEPATTERN");
- this.images = this.prepareImageRange(var3, var4, var1);
- } else {
- var1 = this.getParam("STARTIMAGE");
- if (var1 != null) {
- var3 = Integer.parseInt(var1);
- var1 = this.getParam("NAMEPATTERN");
- this.images = this.prepareImageRange(var3, var4, var1);
- } else {
- var1 = this.getParam("IMAGES");
- if (var1 == null) {
- ((Applet)this).showStatus("No legal IMAGES, STARTIMAGE, or ENDIMAGE specified.");
- this.error = true;
- return;
- }
-
- this.images = this.parseImages(var1, this.getParam("NAMEPATTERN"));
- }
- }
-
- var1 = this.getParam("BACKGROUND");
- if (var1 != null) {
- this.backgroundImageURL = new URL(this.imageSource, var1);
- }
-
- var1 = this.getParam("BACKGROUNDCOLOR");
- if (var1 != null) {
- this.backgroundColor = this.decodeColor(var1);
- }
-
- var1 = this.getParam("STARTUP");
- if (var1 != null) {
- this.startUpImageURL = new URL(this.imageSource, var1);
- }
-
- var1 = this.getParam("SOUNDSOURCE");
- this.soundSource = var1 == null ? this.imageSource : new URL(((Applet)this).getDocumentBase(), var1 + "/");
- var1 = this.getParam("SOUNDS");
- if (var1 != null) {
- this.sounds = this.parseSounds(var1, this.images);
- }
-
- var1 = this.getParam("PAUSES");
- if (var1 != null) {
- this.durations = this.parseDurations(var1, this.images);
- }
-
- var1 = this.getParam("POSITIONS");
- if (var1 != null) {
- this.positions = this.parsePositions(var1, this.images);
- }
-
- var1 = this.getParam("SOUNDTRACK");
- if (var1 != null) {
- this.soundtrackURL = new URL(this.soundSource, var1);
- }
- } catch (MalformedURLException var6) {
- this.showParseError(var6);
- } catch (ParseException var7) {
- this.showParseError(var7);
- }
-
- this.setFrameNum(0);
- }
-
- 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 tellLoadingMsg(String var1, String var2) {
- ((Applet)this).showStatus("Animator: 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 = "Animator: 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 = "Animator: 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 run() {
- Thread var1 = Thread.currentThread();
- var1.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[] var3 = this.tracker.getErrorsAny();
- this.loadErrors(var3, "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 var2 = this.fetchSounds(this.sounds);
- if (var2 != null) {
- this.loadError(var2, "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 var10) {
- this.error = true;
- ((Throwable)var10).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 == var1; this.setFrameNum(this.frameNum + 1)) {
- if (this.frameNum >= this.images.size()) {
- if (!this.repeat) {
- return;
- }
-
- this.setFrameNum(0);
- }
-
- ((Component)this).repaint();
- if (this.sounds != null) {
- AudioClip var5 = (AudioClip)this.sounds.get(this.frameNumKey);
- if (var5 != null) {
- var5.play();
- }
- }
-
- try {
- Integer var12 = null;
- if (this.durations != null) {
- var12 = (Integer)this.durations.get(this.frameNumKey);
- }
-
- if (var12 == null) {
- Thread.sleep((long)this.globalPause);
- } else {
- Thread.sleep((long)var12);
- }
- } catch (InterruptedException var9) {
- }
- }
-
- }
- } 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(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) {
- this.showDescription();
- 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(this.getAppletInfo() + " -- " + "shift-click for errors, info");
- return true;
- case 505:
- ((Applet)this).showStatus("");
- return true;
- default:
- return super.handleEvent(var1);
- }
- }
- }
-