import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.net.*;

public class CursorButton extends Applet implements MouseListener, MouseMotionListener
{
 Color bgcolor[] = new Color[1];
 Color ccolor[]	 = new Color[1];
 Image image[];
 CB cb[];

 int x_image, y_image, w_image, h_image;

 String button, html, target;
 MediaTracker tracker;
 Image image_gr;
 Graphics gr;
 boolean cur_b = false;
 int cur_x, cur_y, u;

 public void init()
 {
  addMouseListener(this); addMouseMotionListener(this);
  tracker = new MediaTracker(this);
  image_gr = createImage(getSize().width, getSize().height);
  gr = image_gr.getGraphics();

   for(int i = 1; (getParameter("button" + i)) != null; i++)
     { 
      image = new Image[i];
      cb    = new CB[i];
     }

   target = getParameter("target");

   StringTokenizer st0 = new StringTokenizer(getParameter("bgcolor"), ",#");
  
   bgcolor[0] = new Color(Integer.parseInt(st0.nextToken()), 
                          Integer.parseInt(st0.nextToken()), 
                          Integer.parseInt(st0.nextToken()));

   setBackground(bgcolor[0]);

   for(int i = 0; (button = getParameter("button" + (i+1))) != null; i++)
     {
      StringTokenizer st1 = new StringTokenizer(button, ",#");

      x_image = Integer.parseInt(st1.nextToken());
      y_image = Integer.parseInt(st1.nextToken());
      w_image = Integer.parseInt(st1.nextToken());
      h_image = Integer.parseInt(st1.nextToken());

      image[i] = getImage(getDocumentBase(), st1.nextToken());
      tracker.addImage(image[i], i);
      try { tracker.waitForID(i); } catch (InterruptedException e) {}

      ccolor[0] = new Color(Integer.parseInt(st1.nextToken()), 
                            Integer.parseInt(st1.nextToken()), 
                            Integer.parseInt(st1.nextToken()));

      html = st1.nextToken();

      cb[i] = new CB(x_image, y_image, w_image, h_image, image[i], ccolor[0], html, this);
     }
  }

 public void paint(Graphics g) { update(g); }

 public void update(Graphics g)
 {
   gr.setColor(getBackground());
   gr.fillRect(0, 0, getSize().width, getSize().height);

   for(int i = 0; i < cb.length; i++)
   {
    cb[i].setImage(gr);
   }

   if(cur_b) cb[u].drawCursor(gr, cur_x, cur_y);

   g.drawImage(image_gr, 0, 0, this);
 }

 public void mouseExited(MouseEvent me)   
 {
  setCursor(Cursor.getDefaultCursor());
  cur_b = false;
  repaint(); 
 }

 public void mouseReleased(MouseEvent me) {}
 public void mouseClicked(MouseEvent me)  {}

 public void mouseMoved(MouseEvent me)    
 {
  cur_x = me.getX();
  cur_y = me.getY();

  for(int i = 0; i < cb.length; i++) 	
     {
      if (cb[i].contains(cur_x, cur_y)) { u = i; }
     }

     if (cb[u].contains(cur_x, cur_y)) 
     { 
      setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
      cur_b = true;
     }

      else {
            setCursor(Cursor.getDefaultCursor());
            cur_b = false;
           }
    
  
  repaint();
 }

 public void mouseDragged(MouseEvent me)  {}
 public void mouseEntered(MouseEvent me)  {}  
 public void mousePressed(MouseEvent me)  
 { 
  if(cur_b && cb[u].contains(cur_x, cur_y)) { cb[u].setURL(target); }

 }

}

class CB extends Canvas
{
 String html; 
 Image image;
 CursorButton cb;
 Color c;
 int x_i, y_i, w_i, h_i;

 public CB(int x_i, int y_i, int w_i, int h_i, Image image, Color c, String html, CursorButton cb) 
  {
   this.x_i 		= x_i;
   this.y_i 		= y_i;
   this.w_i 		= w_i;
   this.h_i 		= h_i;
   this.image		= image;
   this.c               = c;
   this.html            = html;
   this.cb		= cb;
  }

 public void setImage(Graphics g)
  {
   g.drawImage(image, x_i, y_i, w_i, h_i, this);
  }

 public boolean contains(int mouse_x, int mouse_y)
  {
   if (mouse_x >= x_i && mouse_x < x_i+w_i && mouse_y >= y_i && mouse_y < y_i+h_i) return true;
  
   else return false;
  }

 public void setURL(String target)
 {
  try {
	URL url = new URL(cb.getDocumentBase(), html);
	cb.getAppletContext().showDocument(url, target);
       }
	catch (MalformedURLException e) {}
 }

 
 public void drawCursor(Graphics g, int x, int y)
  { 
    g.setColor(c);
    
    g.drawOval(x-2, y-2, 4, 4);

    for(int i = 0; i < 270; i += 45)
    g.drawLine((int)(Math.cos((i*2*Math.PI)/360)*10)+x,
               (int)(Math.sin((i*2*Math.PI)/360)*10)+y,
               (int)(Math.cos((i*2*Math.PI)/360)*7)+x,
               (int)(Math.sin((i*2*Math.PI)/360)*7)+y);

    g.drawString("click", (int)(Math.cos((225*2*Math.PI)/360)*7)+x,
			  (int)(Math.sin((225*2*Math.PI)/360)*7)+y);
  }

}
