/*

ButtonClick.java
Version 1.0.0
Created by Kenny A. Chaffin  4/12/96

Last Updated 4/12/96 by Kenny A. Chaffin

This is a simple clickable image button pressing applet that needs 
two images One normal and another that is "depressed"
the position of the depressed button is adjustable by x and y values
supplied as parameters along with the images as shown here:

<APPLET 
CODE="ButtonClick.class" WIDTH=130 HEIGHT=98>	     // SIZE of button images
<PARAM NAME="image1" VALUE="media/feedback.gif">     // Normal image
<PARAM NAME="image2" VALUE="media/butdown.gif">      // Depressed sub image
<PARAM NAME="x" VALUE="0">			     // x pos. to draw sub-images
<PARAM NAME="y" VALUE="0>			     // y pos. to draw sub-images
<PARAM NAME="dest" VALUE="http://www.digitalempire.com/"   // url to navigate to 
<a Href="someurl"><img src="feedback.gif"></a>             //for non-java browsers
</APPLET>
*/

import java.awt.* ;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.InterruptedException;
import java.applet.Applet;


public class ButtonClick extends java.applet.Applet{

	private	MediaTracker tracker;
	private	Image bg;
	private	Image img[] = new Image[2];
	private	Integer X,Y;
	private	boolean onButt = false;
	private	boolean pressedButt = false;
	private	URL clickDest;
	private	String dest;


	public void init(){

		String istr;

		tracker = new MediaTracker(this);
		
		for (int i = 0; i < 2; i++) 
		  {
		    istr = getParameter("image"+i);
  	            img[i] =  getImage(getCodeBase(),istr);          
		    tracker.addImage(img[i], 0);
		    try 
		      {
			tracker.waitForAll();
		      } 
		    catch (InterruptedException e) 
		      {
			System.out.println("Error waiting for image"+i+" to load");
		      }
	          } 
	          
	        play(getCodeBase(), "media/click.au");

		dest = getParameter("dest");
		X = Integer.valueOf(getParameter("x"));
		Y = Integer.valueOf(getParameter("y"));

		try
		  {
		    clickDest = new URL(dest);
		  }
		catch(MalformedURLException mal)
		  { 
	 	    System.out.println("Malformed URL: Check Applet Tag.");
 		  }	
	}


  public void start()
   	{ repaint(); }

  public void stop() {}

  public void destroy() {}

  public boolean mouseDown(Event e, int x, int y)
    {
 	pressedButt = true;
	repaint();
	return(true);
    }


  public boolean mouseUp(Event e, int x, int y){

	if(pressedButt && onButt)
	  {
		pressedButt = false;
		repaint();
		getAppletContext().showDocument(clickDest);
	  }
	else
	  {
		pressedButt = false;
		repaint();
	  }
	return(true);
  }


  public boolean mouseEnter(Event e, int x, int y)
    {
	onButt = true;
	showStatus(dest);
	repaint();
	return(true);
    }


  public boolean mouseExit(Event e, int x, int y)
    {
	onButt = false;
	showStatus("");
	repaint();
	return(true);
    }
  

  public boolean mouseMove(Event e, int x, int y){
	if (( x <= X.intValue()) && (onButt == true))
	  {
	    onButt = false;
 	    showStatus("");
  	    repaint();
  	  }
  	  
  	if ((x >= X.intValue()) && (onButt == false))
  	  {
  	    onButt = true;
  	    showStatus(dest);
  	    repaint();
  	  }
	return(true);
  }

	public  void update(Graphics g)
          {
	    paint(g);
	  }


	public void paint(Graphics g){
		   paintApplet(g);

	}


	 public void paintApplet(Graphics g) {

		g.drawImage(img[0],0,0,null);   //draw normal image
 		if (onButt)
		   {
		     g.setColor(Color.red);
		     g.drawRect(X.intValue(),Y.intValue(),120,99);
                   }
                   
		if (onButt && pressedButt)
		  {
		    play(getCodeBase(), "media/click.au");
		    g.drawImage(img[1],X.intValue(),Y.intValue(),null);
		  }
	 }
}

