import java.awt.*;
import java.awt.event.*;

public class AzimuthButton extends Panel implements MouseListener {
  SunPath controller;

  int state = 0;

  Image[] offImage = new Image[2];

  String str_value = new String("");
  
  public AzimuthButton(SunPath solar_sim) {
    super();
    setLayout(null);

    this.addMouseListener(this);
    this.controller = solar_sim;

    validate();
  }
  
  public Insets getInsets() {
    return new Insets(4,4,5,5);
  }

  public void mouseClicked(MouseEvent event) {
  }

  public void mousePressed(MouseEvent event) {
    state = 1;
    this.repaint();
  }
  
  public void mouseReleased(MouseEvent event) {
    int coord_x = event.getX();
    int coord_y = event.getY();
    if ((coord_x > 0) && (coord_y > 0) &&
	(coord_x < 80) && (coord_y < 80)) {
      if (controller.sim_panel.draw_azimuth_bool)
	controller.sim_panel.draw_azimuth_bool = false;
      else
	controller.sim_panel.draw_azimuth_bool = true;
      controller.sim_panel.repaint();
    }
    state = 0;
    this.repaint();
  }

  public void mouseEntered(MouseEvent event) {
  }

  public void mouseExited(MouseEvent event) {
  }
  
  public void paint(Graphics g) {
    update(g);
  }
  
  public void update(Graphics g) {
    if ((offImage[0] == null) || (offImage[1] == null)) {
      
      int dim = 80;
      
      // the first image
      offImage[0] = createImage(dim, dim);
      Graphics offGraphics = offImage[0].getGraphics();   
      offGraphics.setColor(new Color(0xbdbdbd));
      offGraphics.fill3DRect(3,3,dim-6,dim-6,true);
      offGraphics.setColor(new Color(0x000000));
      String strAzimuth = new String("Azimuth");
      int strWidth = (offGraphics.getFontMetrics()).stringWidth(strAzimuth);
      offGraphics.drawString(strAzimuth, (dim-strWidth)/2, 40);

      //the second image
      offImage[1] = createImage(dim, dim);
      offGraphics = offImage[1].getGraphics();   
      offGraphics.setColor(new Color(0xbdbdbd));
      offGraphics.fill3DRect(3,3,dim-6,dim-6,false);
      offGraphics.setColor(new Color(0x000000));
      offGraphics.drawString(strAzimuth, (80-strWidth)/2, 40);
    }
    
    g.drawImage(offImage[state], 0, 0, this);
    int strWidthV = (g.getFontMetrics()).stringWidth(str_value);
    g.drawString(str_value, (80-strWidthV)/2, 60);
  }

  public void setButton(double azimuth) {
    int trunc = (int)(azimuth*100.0);
    double azimuth_trunc = (double)trunc/100.0;
    str_value = new String(String.valueOf(azimuth_trunc)+"\u00b0");
    repaint();
  }
}








