import java.applet.Applet; import java.awt.*; import netscape.javascript.JSObject; // This appears in Core Web Programming from // Prentice Hall Publishers, and may be freely used // or adapted. 1997 Marty Hall, hall@apl.jhu.edu. /** An applet that draws a mountain image and displays * a slider. Dragging the slider changes a textfield * in the HTML file containing the applet. Moving the * mouse over the image changes another textfield 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 declaration. * Works only in Netscape, in version 3.0 * or later.. */ 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 = size().width; height = size().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("South", costSlider); // Get references to HTML textfields via 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); } /** When user moves the mouse, scale the y value * from 29000 (top) to 0 (bottom) and send it * to external textfield through JavaScript. */ public boolean mouseMove(Event event, int x, int y) { setAltitudeField((height - y) * 29000 / height); return(true); } /** Change textfield via JavaScript. */ public void setCostField(int val) { costField.setMember("value", String.valueOf(val)); } /** Change textfield via JavaScript. */ private void setAltitudeField(int val) { altitudeField.setMember("value", String.valueOf(val)); } }