// Developed By Anshoo for Captain Java.
// http://www.geocities.com/SiliconValley/Heights/3413
// Please modify or use this code as you want. No need to Give me
// any credit, Commercial or Non Commercial use of Code or Applet is
// hereby granted. A visit to my site and a entry in Guestbook will
// do just fine. but it is not condition, it is upto you.

import java.awt.*;
import java.net.*;
import java.lang.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.InputStream;

public class AnimSndButton extends java.applet.Applet{

boolean onTheButton = false;    // Flag if mouse is one the image
boolean PressedButton = false;  // Flag set if mouse pressed on image
int BttTrck = 0;                // Keeps Track of the current image.
URL DestURL;                    // URL to open on Click.
String destination;             // Destination URL.
String target;                  // Target frame or window.
String status;                  // Text to show on Status bar when mouse is on image
AudioClip playList[];           // Array of Audio clips to play.
MediaTracker mt;                // media Tracker
Image images[];                 // Array of images to display


public void init()
{
getMedia();                       // Load all images and audio clips
target = getParameter("target");  // Get target  Parameter
status = getParameter("status");  // Get status  Parameter
destination = getParameter("destination");   // Get destination Parameter
    try{
       DestURL = new URL(destination);     // get URL of destination
       }catch(MalformedURLException mal){
              System.out.println("Malformed URL"); // Check validity of URL
       }
}

  public void start(){

        repaint();              // Repaint Graphics
  }

  public void stop(){
  }

  public void destroy(){
  }

  public boolean mouseDown(Event e, int x, int y){
        PressedButton = true;       // if mouse pressed then Pressed Flag on
        repaint();               // Repaint images

        return(true);
  }

  public boolean mouseUp(Event e, int x, int y){

        if(PressedButton && onTheButton){
                PressedButton = false;  // if button pressed and is on the image
                repaint();             // then repaint images 
                if (target == null)   // if target not specified load in new Window
                        getAppletContext().showDocument(DestURL);
                else   // Load Document in Specified target
                        getAppletContext().showDocument(DestURL,target);
        }else{
                PressedButton = false;   // if Pressed but not on image then 
                repaint();              // pressed flag off, repaint images
        }
        return(true);
  }

  public boolean mouseEnter(Event e, int x, int y){
        onTheButton = true;    // if mouse is on the image
        if (status== null)                  // and status not defined 
                showStatus(destination);    // then show destination URL
        else
                showStatus(status);    // else show Status

        repaint();                      // Repaint images

        return(true);
  }

  public boolean mouseExit(Event e, int x, int y){
        onTheButton = false;    // if mouse moves out of image
        showStatus("");         // on the button flag off and status off
        repaint();              // repaint images

        return(true);
  }

   public void update(Graphics g){

                if(!onTheButton)        // if mouse is not on the image
                        BttTrck = 0;    // then image index points to normal image
                else if (onTheButton && !PressedButton) // if mouse is on the image
                        BttTrck = 2;   // and pressed then show Pressed image
                else
                        BttTrck = 1;    // else show hilighted image

                        paint(g);
        }

        public void paint(Graphics g){
                g.drawImage(images[BttTrck], 0,0,this);
                if(BttTrck != 0){
                        playList[BttTrck-1].play(); // play sound according to image state
                    }
        }

private synchronized void getMedia() {
mt = new MediaTracker(this);
playList = new AudioClip[2];
for(int i = 0; i < 2; i++)
{
try
{
playList[i]= getAudioClip(getDocumentBase(),  // load all sounds
getParameter("AUDIO"+(i+1)));
} catch (Exception e)
{
System.out.println(e);
}
}

images = new Image[3];
String location;
for(int j = 0; j < 3; j++)
{
try
{
        location = getParameter("image"+j);
        images[j] =  getImage(getCodeBase(),location);   // load all images
} catch(Exception e1)
{
        System.out.println(e1);
}

mt.addImage(images[j], j);      // add images to media tracker
try
{
mt.checkID(j, true);
mt.waitForID(j);
showStatus("Loading images...");
} catch(InterruptedException e3)
{
System.out.println(e3);
}
}
}


}
