home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Extras / Content / Images / Modomedia / LoopSound.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-11  |  4.6 KB  |  251 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Component;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.MediaTracker;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10.  
  11. public class LoopSound extends Applet implements Runnable {
  12.    MediaTracker tracker;
  13.    boolean inited = false;
  14.    Thread tickTock;
  15.    AudioClip theSound;
  16.    int soundOnOrOff;
  17.    Image soundOnImage;
  18.    Image soundOffImage;
  19.    boolean mouseOut = true;
  20.    boolean mouseIsDown = false;
  21.    boolean notUpdatedYet = true;
  22.  
  23.    public String getAppletInfo() {
  24.       return new String("LoopSound 1.0 (c) J. Criswick - criswick@conveyor.com, Mar.25.96");
  25.    }
  26.  
  27.    public void init() {
  28.       this.tracker = new MediaTracker(this);
  29.       String var6 = ((Applet)this).getParameter("on-icon");
  30.       String var1 = var6 == null ? "sound-on.gif" : var6;
  31.       var6 = ((Applet)this).getParameter("off-icon");
  32.       String var2 = var6 == null ? "sound-off.gif" : var6;
  33.       var6 = ((Applet)this).getParameter("volume");
  34.       this.soundOnOrOff = var6 == null ? 1 : Integer.valueOf(var6);
  35.       if (this.soundOnOrOff != 0 && this.soundOnOrOff != 1) {
  36.          this.soundOnOrOff = 1;
  37.       }
  38.  
  39.       var6 = ((Applet)this).getParameter("sound");
  40.       URL var3 = null;
  41.       URL var4 = null;
  42.       Object var5 = null;
  43.  
  44.       try {
  45.          var3 = this.MakeAbsoluteURL(var1);
  46.          var4 = this.MakeAbsoluteURL(var2);
  47.          this.MakeAbsoluteURL(var6);
  48.       } catch (Exception var11) {
  49.          System.out.println("A problem occurred with one of the URLs.");
  50.       }
  51.  
  52.       if (var4 != null) {
  53.          try {
  54.             this.soundOffImage = ((Applet)this).getImage(var4);
  55.             this.tracker.addImage(this.soundOffImage, 0);
  56.             this.tracker.waitForID(0);
  57.          } catch (InterruptedException var10) {
  58.             System.out.println("A problem occurred in loading the on image");
  59.          }
  60.       }
  61.  
  62.       if (var3 != null) {
  63.          try {
  64.             this.soundOnImage = ((Applet)this).getImage(var3);
  65.             this.tracker.addImage(this.soundOnImage, 1);
  66.             this.tracker.waitForID(1);
  67.          } catch (InterruptedException var9) {
  68.             System.out.println("A problem occurred in loading the on image");
  69.          }
  70.       }
  71.  
  72.       var6 = ((Applet)this).getParameter("width");
  73.       int var7 = var6 == null ? 32 : Integer.valueOf(var6);
  74.       var6 = ((Applet)this).getParameter("height");
  75.       int var8 = var6 == null ? 32 : Integer.valueOf(var6);
  76.       ((Applet)this).resize(var7, var8);
  77.    }
  78.  
  79.    private URL MakeAbsoluteURL(String var1) throws MalformedURLException {
  80.       URL var2;
  81.       try {
  82.          var2 = new URL(var1);
  83.       } catch (MalformedURLException var3) {
  84.          var2 = new URL(((Applet)this).getDocumentBase(), var1);
  85.       }
  86.  
  87.       return var2;
  88.    }
  89.  
  90.    public void paint(Graphics var1) {
  91.       if (this.soundOnOrOff == 0) {
  92.          if (this.tracker.checkID(0)) {
  93.             var1.drawImage(this.soundOffImage, 0, 0, this);
  94.             return;
  95.          }
  96.       } else if (this.tracker.checkID(1)) {
  97.          var1.drawImage(this.soundOnImage, 0, 0, this);
  98.       }
  99.  
  100.    }
  101.  
  102.    public boolean mouseMove(Event var1, int var2, int var3) {
  103.       if (this.notUpdatedYet) {
  104.          this.notUpdatedYet = false;
  105.          if (this.soundOnOrOff == 0) {
  106.             ((Applet)this).showStatus("Turn sound on");
  107.          } else {
  108.             ((Applet)this).showStatus("Turn sound off");
  109.          }
  110.       }
  111.  
  112.       return true;
  113.    }
  114.  
  115.    public boolean mouseDrag(Event var1, int var2, int var3) {
  116.       if (this.notUpdatedYet) {
  117.          this.notUpdatedYet = false;
  118.          if (this.mouseOut) {
  119.             if (this.soundOnOrOff == 0) {
  120.                ((Applet)this).showStatus("Leave sound off");
  121.             } else {
  122.                ((Applet)this).showStatus("Leave sound on");
  123.             }
  124.          } else if (this.soundOnOrOff == 0) {
  125.             ((Applet)this).showStatus("Turn sound on");
  126.          } else {
  127.             ((Applet)this).showStatus("Turn sound off");
  128.          }
  129.       }
  130.  
  131.       return true;
  132.    }
  133.  
  134.    public boolean mouseEnter(Event var1, int var2, int var3) {
  135.       Graphics var4 = ((Component)this).getGraphics();
  136.       if (this.mouseIsDown) {
  137.          if (this.soundOnOrOff == 0) {
  138.             if (this.tracker.checkID(1)) {
  139.                var4.drawImage(this.soundOnImage, 0, 0, this);
  140.             }
  141.          } else if (this.tracker.checkID(0)) {
  142.             var4.drawImage(this.soundOffImage, 0, 0, this);
  143.          }
  144.       }
  145.  
  146.       if (this.soundOnOrOff == 0) {
  147.          ((Applet)this).showStatus("Turn sound on");
  148.       } else {
  149.          ((Applet)this).showStatus("Turn sound off");
  150.       }
  151.  
  152.       this.mouseOut = false;
  153.       return true;
  154.    }
  155.  
  156.    public boolean mouseDown(Event var1, int var2, int var3) {
  157.       Graphics var4 = ((Component)this).getGraphics();
  158.       this.mouseIsDown = true;
  159.       if (this.soundOnOrOff == 0) {
  160.          ((Applet)this).showStatus("Turn sound on");
  161.          if (this.tracker.checkID(1)) {
  162.             var4.drawImage(this.soundOnImage, 0, 0, this);
  163.          }
  164.       } else {
  165.          ((Applet)this).showStatus("Turn sound off");
  166.          if (this.tracker.checkID(0)) {
  167.             var4.drawImage(this.soundOffImage, 0, 0, this);
  168.          }
  169.       }
  170.  
  171.       return true;
  172.    }
  173.  
  174.    public boolean mouseUp(Event var1, int var2, int var3) {
  175.       this.mouseIsDown = false;
  176.       if (!this.mouseOut) {
  177.          if (this.soundOnOrOff == 0) {
  178.             this.soundOnOrOff = 1;
  179.             ((Applet)this).showStatus("Turn sound off");
  180.             this.start();
  181.          } else {
  182.             this.soundOnOrOff = 0;
  183.             ((Applet)this).showStatus("Turn sound on");
  184.             this.stop();
  185.          }
  186.       }
  187.  
  188.       this.paint(((Component)this).getGraphics());
  189.       return true;
  190.    }
  191.  
  192.    public boolean mouseExit(Event var1, int var2, int var3) {
  193.       Graphics var4 = ((Component)this).getGraphics();
  194.       this.notUpdatedYet = true;
  195.       this.mouseOut = true;
  196.       if (this.mouseIsDown) {
  197.          if (this.soundOnOrOff == 0) {
  198.             ((Applet)this).showStatus("Leave sound off");
  199.             if (this.tracker.checkID(1)) {
  200.                var4.drawImage(this.soundOffImage, 0, 0, this);
  201.             }
  202.          } else {
  203.             ((Applet)this).showStatus("Leave sound on");
  204.             if (this.tracker.checkID(0)) {
  205.                var4.drawImage(this.soundOnImage, 0, 0, this);
  206.             }
  207.          }
  208.       } else {
  209.          ((Applet)this).showStatus("");
  210.       }
  211.  
  212.       return true;
  213.    }
  214.  
  215.    public void start() {
  216.       if (this.tickTock == null) {
  217.          this.tickTock = new Thread(this);
  218.          this.tickTock.start();
  219.          if (this.theSound != null && this.soundOnOrOff == 1) {
  220.             this.theSound.loop();
  221.             return;
  222.          }
  223.       } else if (this.theSound != null && this.soundOnOrOff == 1) {
  224.          this.theSound.loop();
  225.       }
  226.  
  227.    }
  228.  
  229.    public void stop() {
  230.       if (this.tickTock != null) {
  231.          this.tickTock.stop();
  232.          this.tickTock = null;
  233.          this.theSound.stop();
  234.       }
  235.  
  236.    }
  237.  
  238.    public void run() {
  239.       if (!this.inited) {
  240.          try {
  241.             this.theSound = ((Applet)this).getAudioClip(new URL(((Applet)this).getDocumentBase(), ((Applet)this).getParameter("sound")));
  242.          } catch (MalformedURLException var1) {
  243.          }
  244.  
  245.          this.inited = true;
  246.          this.theSound.loop();
  247.       }
  248.  
  249.    }
  250. }
  251.