home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / converte.jav < prev    next >
Encoding:
Text File  |  1996-02-26  |  6.8 KB  |  239 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /* This program could use some layout work, and the functionality
  18.  * could use some tweaking, but it seems to basically work.
  19.  */
  20. import java.awt.*;
  21. import java.util.*;
  22. import java.applet.Applet;
  23.  
  24. public class Converter extends Applet {
  25.     Frame window;
  26.     ConversionPanel metricPanel, usaPanel;
  27.     Unit metricDistances[] = new Unit[3];
  28.     Unit usaDistances[] = new Unit[4];
  29.  
  30.     /** Create the ConversionPanels (one for metric, another for U.S.).
  31.       * I used "U.S." because although Imperial and U.S. distance
  32.       * measurements are the same, this program could be extended to
  33.       * include volume measurements, which aren't the same.
  34.       */
  35.     public void init() {
  36.     setLayout(new GridLayout(2,0,5,5));
  37.  
  38.     metricDistances[0] = new Unit("Centimeters", 0.01);
  39.     metricDistances[1] = new Unit("Meters", 1.0);
  40.     metricDistances[2] = new Unit("Kilometers", 1000.0);
  41.     metricPanel = new ConversionPanel(this, "Metric System",
  42.                         metricDistances);
  43.  
  44.     usaDistances[0] = new Unit("Inches", 0.0254);
  45.     usaDistances[1] = new Unit("Feet", 0.305);
  46.     usaDistances[2] = new Unit("Yards", 0.914);
  47.     usaDistances[3] = new Unit("Miles", 1613.0);
  48.     usaPanel = new ConversionPanel(this, "U.S. System", usaDistances);
  49.  
  50.     add(metricPanel);
  51.     add(usaPanel);
  52.     }
  53.  
  54.     /** Does the conversion from metric to U.S., or vice versa, and
  55.       * updates the appropriate ConversionPanel. */
  56.     void convert(ConversionPanel from) {
  57.     ConversionPanel to;
  58.  
  59.     if (from == metricPanel)
  60.         to = usaPanel;
  61.     else
  62.         to = metricPanel;
  63.     double multiplier = from.getMultiplier() / to.getMultiplier();
  64.     to.setValue(from.getValue() * multiplier);
  65.     }
  66.  
  67.     /** Draws a box around this panel. */
  68.     public void paint(Graphics g) {
  69.     Dimension d = size();
  70.     g.drawRect(0,0, d.width - 1, d.height - 1);
  71.     }
  72.     
  73.     /** Puts a little breathing space between
  74.       * the panel and its contents, which lets us draw a box
  75.       * in the paint() method.
  76.       */
  77.     public Insets insets() {
  78.     return new Insets(5,5,5,5);
  79.     }
  80.  
  81.     public static void main(String args[]) {
  82.     Frame f = new Frame("Converter Applet/Application");
  83.     Converter converter = new Converter();
  84.  
  85.     converter.init();
  86.  
  87.     f.add("Center", converter);
  88.     f.pack();
  89.     f.show();
  90.     }
  91.  
  92. }
  93.  
  94.  
  95. class ConversionPanel extends Panel {
  96.     String title;
  97.     TextField textField;
  98.     Scrollbar slider;
  99.     Choice unitChooser;
  100.     int min = 0;
  101.     int max = 10000;
  102.     Converter controller;
  103.     Unit units[];
  104.  
  105.     //TO DO: Should make both panels' choices the same width.
  106.     ConversionPanel(Converter myController, String myTitle, Unit myUnits[]) {
  107.     super();
  108.     GridBagConstraints c = new GridBagConstraints();
  109.     GridBagLayout gridbag = new GridBagLayout();
  110.     setLayout(gridbag);
  111.     controller = myController;
  112.     title = myTitle;
  113.     units = myUnits;
  114.  
  115.     //Set up default constraints
  116.     c.fill = GridBagConstraints.HORIZONTAL;
  117.  
  118.     //Add the label
  119.     Label label = new Label(title, Label.CENTER);
  120.     c.weightx = 0.0;
  121.     c.gridwidth = GridBagConstraints.REMAINDER;
  122.     gridbag.setConstraints(label, c);
  123.     add(label);
  124.  
  125.     //Add the text field
  126.     textField = new TextField("0", 10);
  127.     c.weightx = 1.0;
  128.     c.gridwidth = GridBagConstraints.RELATIVE;
  129.     gridbag.setConstraints(textField, c);
  130.     add(textField);
  131.  
  132.     //Add the pop-up list (Choice)
  133.     unitChooser = new Choice();
  134.     for (int i = 0; i < units.length; i++) {
  135.         unitChooser.addItem(units[i].description);
  136.     }
  137.     c.weightx = 0.0;
  138.     c.gridwidth = GridBagConstraints.REMAINDER;
  139.     gridbag.setConstraints(unitChooser, c);
  140.     add(unitChooser);
  141.  
  142.     //Add the slider
  143.     slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 100, min, max);
  144.     c.weightx = 0.0;
  145.     c.gridheight = 1;
  146.     c.gridwidth = GridBagConstraints.RELATIVE;
  147.     gridbag.setConstraints(slider, c);
  148.     add(slider);
  149.     }
  150.  
  151.     /** Returns the multiplier (units/meter) for the currently
  152.       * selected unit of measurement.
  153.       */
  154.     double getMultiplier() {
  155.     int i = unitChooser.getSelectedIndex();
  156.     return (units[i].multiplier);
  157.     }
  158.  
  159.     /** Draws a box around this panel. */
  160.     public void paint(Graphics g) {
  161.     Dimension d = size();
  162.     g.drawRect(0,0, d.width - 1, d.height - 1);
  163.     }
  164.     
  165.     /** Puts a little breathing space between
  166.       * the panel and its contents, which lets us draw a box
  167.       * in the paint() method.
  168.       * We add more pixels to the right, to work around a
  169.       * Choice bug.
  170.       */
  171.     public Insets insets() {
  172.     return new Insets(5,5,5,8);
  173.     }
  174.  
  175.     /** Gets the current value in the text field.
  176.       * That's guaranteed to be the same as the value
  177.       * in the scroller (subject to rounding, of course).
  178.       */
  179.     double getValue() {
  180.     double f;
  181.     try {
  182.         f = Double.valueOf(textField.getText()).doubleValue(); 
  183.     } catch (java.lang.NumberFormatException e) {
  184.         f = 0.0;
  185.     }
  186.     return f;
  187.     }
  188.  
  189.     /** Respond to user actions. */
  190.     public boolean handleEvent(Event e) {
  191.     if (e.target instanceof Scrollbar) {
  192.         textField.setText(String.valueOf(slider.getValue()));
  193.         controller.convert(this);
  194.     } else if ((e.target instanceof TextField) 
  195.            && (e.id == Event.ACTION_EVENT)) {
  196.         setSliderValue(getValue());
  197.         controller.convert(this);
  198.     } else if ((e.target instanceof Choice) 
  199.            && (e.id == Event.ACTION_EVENT)) {
  200.         controller.convert(this);
  201.     } 
  202.     return false;
  203.     }
  204.  
  205.     /** Set the values in the slider and text field. */
  206.     void setValue(double f) {
  207.     setSliderValue(f);
  208.         textField.setText(String.valueOf(f));
  209.     }
  210.  
  211.     /** Set the slider value. */
  212.     void setSliderValue(double f) {
  213.     int sliderValue = (int)f;
  214.  
  215.     if (sliderValue > max)
  216.            sliderValue = max;
  217.     if (sliderValue < min)
  218.         sliderValue = min;
  219.         slider.setValue(sliderValue);
  220.     }
  221. }
  222.  
  223.  
  224. class Unit {
  225.     String description;
  226.     double multiplier;
  227.  
  228.     Unit(String description, double multiplier) {
  229.     super();
  230.     this.description = description;
  231.     this.multiplier = multiplier;
  232.     }
  233.  
  234.     public String toString() {
  235.     String s = "Meters/" + description + " = " + multiplier;
  236.     return s;
  237.     }
  238. }
  239.