import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.JSObject;

/** An applet that draws a mountain image and displays
 *  a slider. Dragging the slider changes a text field
 *  in the HTML file containing the applet. Moving the
 *  mouse over the image changes another text field in the
 *  containing HTML file, using an altitude value
 *  where the bottom of the applet corresponds to 0
 *  and the top to 29000 feet. This requires
 *  an HTML file with a form named "highPeaksForm"
 *  containing two text fields: one named "costField"
 *  and one named "altitudeField". It also requires use
 *  of the MAYSCRIPT tag in the <APPLET ...> declaration.
 *
 *  Taken from Core Web Programming from 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted. 
 */

public class Everest extends Applet {
  private Image mountain;
  private JSObject window, document, highPeaksForm,
          costField, altitudeField;
  private int width, height;

  public void init() {
    setBackground(Color.lightGray);
    mountain = getImage(getCodeBase(), "images/peak5.gif");
    width = getSize().width;
    height = getSize().height;
    // Start image loading immediately.
    prepareImage(mountain, width, height, this);
    setLayout(new BorderLayout());
    Font sliderFont = new Font("Helvetica", Font.BOLD, 18);
    LabeledCostSlider costSlider =
      new LabeledCostSlider("Specify a maximum cost:", 
                            sliderFont, 2000, 20000, 5000, 
                            this);
    add(costSlider, BorderLayout.SOUTH);
    addMouseMotionListener(new MouseMotionAdapter() {
      // When user moves the mouse, scale the y value
      // from 29000 (top) to 0 (bottom) and send it
      // to external text field through JavaScript.
      public void mouseMoved(MouseEvent event) {
        System.out.println("Mouse Move at : " + event.getY());
        setAltitudeField((height - event.getY()) * 29000 / height);
      }
    });
        
    // Get references to HTML text fields through JavaScript.
    window = JSObject.getWindow(this); // this=applet
    document = (JSObject)window.getMember("document");
    highPeaksForm = 
      (JSObject)document.getMember("highPeaksForm");
    costField =
      (JSObject)highPeaksForm.getMember("costField");
    altitudeField =
      (JSObject)highPeaksForm.getMember("altitudeField");
    setCostField(5000);
    setAltitudeField(15000);
  }

  public void paint(Graphics g) {
    g.drawImage(mountain, 0, 0, width, height, this);
  }
  
  /** Change text field through JavaScript. */
  
  public void setCostField(int val) {
    costField.setMember("value", String.valueOf(val));
  }

  /** Change text field through JavaScript. */
  
  private void setAltitudeField(int val) {
    altitudeField.setMember("value", String.valueOf(val));
  } 
}