/*========================================================
 The Button (hbutton): By E.Sanchez Velasco  05/28/97
 Draws a button in the screen. When the button is pressed,
 it will put you in the HTML link declared in the PARAMETER 
 <PARAM NAME="link" VALUE="link.html">, where link.html
 is a HTML file located at URL base given in the PARAMETER
 <PARAM NAME="urlbase" VALUE="http://www.yoururl.org"> 
(replace the last value with yours). If no urlbase parameter
 is given, then the url base is taken to be the same as the 
 url base of the applet. You can make the button any size.
 Example of how to call the applet:
 <applet code="hbutton.class" width=18 height=15 hspace=5>
 <PARAM NAME="link" VALUE="yourlink.html">
 <PARAM NAME="urlbase" VALUE="http://www.yoururl.com">
 </applet>
==========================================================*/
import java.awt.*;
import java.applet.*;
import java.net.*;

public class hbutton extends Applet
{
   int W, H;                 // Widht and Height of applet
   boolean Btn;              // Button status
   String theLink;           // Link to go from HTML file              
   
   // ----------------- METHODS ----------------------------
    
   public void init()
   { 
     // Get the link to go when button is pressed
     theLink = getParameter("link"); 
     if(theLink!=null)
     {
        String urlbase = getParameter("urlbase"); 
        if(urlbase==null)
        theLink = getCodeBase()+theLink; 
        else
        theLink = urlbase+"/"+theLink; 
     }
       
     // Set background color 
     setBackground(Color.lightGray);

     // Get the dimensions of plot area
     W = size().width; 
     H = size().height; 
     
     // Button is up
     Btn = false;
      
   } //--------- End of init() -------------- 
    
   // Start just paints the button up 
   public void start()
   {
      Btn = false; 
      repaint();
      
   }  // End of start()

   public final synchronized void update(Graphics g)
   {
      paint(g);
      
   } //--------- End of update() --------------
   
   public void paint( Graphics g )
   {   
      int border1x[] = {0,0,W-1,W-3,2,2};
      int border1y[] = {H-1,0,0,2,2,H-3};
      int border2x[] = {0,W-1,W-1,W-3,W-3,2};
      int border2y[] = {H-1,H-1,0,2,H-3,H-3};

      // Draw the button area
      g.setColor(Color.lightGray);
      g.fillRect(3,3,W-6,H-6);
      
      // Draw the border of the button
      if(Btn) g.setColor(Color.darkGray);
      else    g.setColor(Color.white);
      g.fillPolygon(border1x,border1y,6); 
      g.drawPolygon(border1x,border1y,6);
      
      if(Btn) g.setColor(Color.white);
      else    g.setColor(Color.darkGray);
      g.fillPolygon(border2x,border2y,6); 
      g.drawPolygon(border2x,border2y,6);

   } //--------- End of paint() ---------
   
   // Handle a key pressed in the mouse
   public boolean mouseDown(Event click, int x, int y) 
   {
     
     if( !Btn )           // If button is up put it down
     {
       Btn = true;
       repaint();
     }
     
     return true;
   
   } //--------- End of mouseDown() ---------- 
  
   public boolean mouseUp(Event click, int x, int y) 
   {
     
     if( Btn )           // If button is down
     {
       Btn = false;                     // Make button up
       if( theLink != null )
       {
          URL theURL;
          try{ theURL = new URL(theLink);}
          catch(MalformedURLException e)
          {repaint(); return true; }
           
          // Go to the link (at applet document base)
          getAppletContext().showDocument( theURL );
       }
       
       // If no link, repaint the button up
       repaint();
     }
     
     return true;
   
   } //--------- End of mouseUp() ---------- 
  
   public boolean mouseExit(Event click, int x, int y) 
   {
     
     if( Btn )           // If button is down put it up
     {
       Btn = false;
       repaint();
     }
     
     return true;
   
   } //--------- End of mouseExit() ---------- 
   
}