import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;

/**
 * Title:        Animated Button
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      BS
 * @author Willian Charles Balmant
 * @version 1.0
 */

public class AnimatedButton extends Applet implements Runnable {
  Image img = null;
  Image image = null;
  Image effectImage = null;
  String href = null;
  String target = null;
  String imageStr = null;
  String effectImageStr = null;
  Cursor handCursor, defaultCursor;
  boolean isStandalone = false;

  Thread thread = new Thread(this);
  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  public AnimatedButton() {
  }

  /** Initializa o applet*/
  public void init() {
		/* obtendo o url da imagem, imagem de efeito, link, e target do código HTML da página */
    try {
      imageStr = this.getParameter("image", "");
      effectImageStr = this.getParameter("effectimage", "");
      this.href = this.getParameter("href", "");
      this.target = this.getParameter("target", "");
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    /* para manipular eventos de mouse */
    this.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				mouseEnteredAction(e);
		  }
		  public void mouseExited(MouseEvent e) {
		    mouseExitedAction(e);
		  }
		  public void mouseReleased(MouseEvent e) {
		    mouseReleasedAction(e);
			}
    });
    /* thread que monitora o download das imagens */
    thread.start();
  }

  /**Start the applet*/
  public void start() {
    handCursor = new Cursor(Cursor.HAND_CURSOR);
    defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
  }

  public void run() {
    if(imageStr != null)
        image = getImage(getCodeBase(), imageStr);
    if(effectImageStr != null)
        effectImage = getImage(getCodeBase(), effectImageStr);
    img = image;
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image,0);
    mt.addImage(effectImage,1);
    while(!mt.checkAll()) {
      try {
        thread.sleep(500);
        repaint();
      } catch(InterruptedException e) {
      }
    }
  }

  /** Método que faz o rollover para a imagem de efeito (onmouseover) */
  private void mouseEnteredAction(MouseEvent e) {
    img = effectImage; // muda a imagem pintada para a imagem de efeito
    // se o applet esta linkado muda o cursor do mouse
    if(href != null);
      setCursor(handCursor);
    repaint(); // chama o método paint()
  }

	/** Método que faz o rollover para a imagem normal (onmouseout) */
  private void mouseExitedAction(MouseEvent e) {
    img = image; // muda a imagem pintada para a imagem normal
    setCursor(defaultCursor); // muda o cursor o default
    repaint(); // chama o método paint()
  }

	/** Se o mouse for liberado sobre o botão redireciona <br>
	 *  a página atual para href
	 */
  private void mouseReleasedAction(MouseEvent e) {
    if(getAppletContext() == null || href == null) return;
    try {
      if(target == null)
        getAppletContext().showDocument(new URL(href));
      else
        getAppletContext().showDocument(new URL(href), target);
    }
    catch(MalformedURLException exception) {
       exception.printStackTrace();
    }
  }

  /** método que pinta a imagem do applet */
  public void paint(Graphics g) {
    if (img == null) return;
    g.drawImage(img, 0, 0, this);
  }
}
