FileDocCategorySizeDatePackage
Converter.javaAPI DocExample3274Tue Dec 12 18:58:50 GMT 2000None

Converter

public class Converter extends Applet

Fields Summary
ConversionPanel
metricPanel
ConversionPanel
usaPanel
Unit[]
metricDistances
Unit[]
usaDistances
Constructors Summary
Methods Summary
voidconvert(ConversionPanel from)
Does the conversion from metric to U.S., or vice versa, and updates the appropriate ConversionPanel.

        ConversionPanel to;

        if (from == metricPanel)
            to = usaPanel;
        else
            to = metricPanel;

        double multiplier = from.getMultiplier() / to.getMultiplier();
        to.setValue(multiplier * from.getValue());
    
public java.awt.InsetsgetInsets()
Puts a little breathing space between the panel and its contents, which lets us draw a box in the paint() method.

        return new Insets(5,5,5,5);
    
public voidinit()
Create the ConversionPanels (one for metric, another for U.S.). I used "U.S." because although Imperial and U.S. distance measurements are the same, this program could be extended to include volume measurements, which aren't the same.


                                             
       
        //Use a GridLayout with 2 rows, as many columns as necessary,
        //and 5 pixels of padding around all edges of each cell.
        setLayout(new GridLayout(2,0,5,5));

        //Create Unit objects for metric distances, and then 
        //instantiate a ConversionPanel with these Units.
        metricDistances[0] = new Unit("Centimeters", 0.01);
        metricDistances[1] = new Unit("Meters", 1.0);
        metricDistances[2] = new Unit("Kilometers", 1000.0);
        metricPanel = new ConversionPanel(this, "Metric System",
                                            metricDistances);

        //Create Unit objects for U.S. distances, and then 
        //instantiate a ConversionPanel with these Units.
        usaDistances[0] = new Unit("Inches", 0.0254);
        usaDistances[1] = new Unit("Feet", 0.305);
        usaDistances[2] = new Unit("Yards", 0.914);
        usaDistances[3] = new Unit("Miles", 1613.0);
        usaPanel = new ConversionPanel(this, "U.S. System", usaDistances);

        //Add both ConversionPanels to the Converter.
        add(metricPanel);
        add(usaPanel);
    
public static voidmain(java.lang.String[] args)
Executed only when this program runs as an application.

        //Create a new window.
        Frame f = new Frame("Converter Applet/Application");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //Create a Converter instance.
        Converter converter = new Converter();

        //Initialize the Converter instance.
        converter.init();

        //Add the Converter to the window and display the window.
        f.add("Center", converter);
        f.pack();        //Resizes the window to its natural size.
        f.setVisible(true);
    
public voidpaint(java.awt.Graphics g)
Draws a box around this panel.

        Dimension d = getSize();
        g.drawRect(0,0, d.width - 1, d.height - 1);