samer@1: /* samer@1: * 1.1+Swing version. samer@1: */ samer@1: samer@1: import javax.swing.*; samer@1: import javax.swing.event.*; samer@1: import java.awt.*; samer@1: import java.awt.event.*; samer@1: import java.util.*; samer@1: samer@1: public class Converter { samer@1: ConversionPanel metricPanel, usaPanel; samer@1: Unit[] metricDistances = new Unit[3]; samer@1: Unit[] usaDistances = new Unit[4]; samer@1: final static boolean COLORS = false; samer@1: final static boolean DEBUG = false; samer@1: final static String LOOKANDFEEL = null; samer@1: ConverterRangeModel dataModel = new ConverterRangeModel(); samer@1: JPanel mainPane; samer@1: samer@1: /** samer@1: * Create the ConversionPanels (one for metric, another for U.S.). samer@1: * I used "U.S." because although Imperial and U.S. distance samer@1: * measurements are the same, this program could be extended to samer@1: * include volume measurements, which aren't the same. samer@1: * samer@1: * Put the ConversionPanels into a frame, and bring up the frame. samer@1: */ samer@1: public Converter() { samer@1: //Create Unit objects for metric distances, and then samer@1: //instantiate a ConversionPanel with these Units. samer@1: metricDistances[0] = new Unit("Centimeters", 0.01); samer@1: metricDistances[1] = new Unit("Meters", 1.0); samer@1: metricDistances[2] = new Unit("Kilometers", 1000.0); samer@1: metricPanel = new ConversionPanel(this, "Metric System", samer@1: metricDistances, samer@1: dataModel); samer@1: samer@1: //Create Unit objects for U.S. distances, and then samer@1: //instantiate a ConversionPanel with these Units. samer@1: usaDistances[0] = new Unit("Inches", 0.0254); samer@1: usaDistances[1] = new Unit("Feet", 0.305); samer@1: usaDistances[2] = new Unit("Yards", 0.914); samer@1: usaDistances[3] = new Unit("Miles", 1613.0); samer@1: usaPanel = new ConversionPanel(this, "U.S. System", samer@1: usaDistances, samer@1: new FollowerRangeModel(dataModel)); samer@1: samer@1: //Create a JPanel, and add the ConversionPanels to it. samer@1: mainPane = new JPanel(); samer@1: if (COLORS) { samer@1: mainPane.setBackground(Color.red); samer@1: } samer@1: mainPane.setLayout(new GridLayout(2,1,5,5)); samer@1: mainPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); samer@1: mainPane.add(metricPanel); samer@1: mainPane.add(usaPanel); samer@1: resetMaxValues(true); samer@1: } samer@1: samer@1: public void resetMaxValues(boolean resetCurrentValues) { samer@1: double metricMultiplier = metricPanel.getMultiplier(); samer@1: double usaMultiplier = usaPanel.getMultiplier(); samer@1: int maximum = ConversionPanel.MAX; samer@1: samer@1: if (metricMultiplier > usaMultiplier) { samer@1: maximum = (int)(ConversionPanel.MAX * samer@1: (usaMultiplier/metricMultiplier)); samer@1: } samer@1: samer@1: if (DEBUG) { samer@1: System.out.println("in Converter resetMaxValues"); samer@1: System.out.println(" metricMultiplier = " samer@1: + metricMultiplier samer@1: + "; usaMultiplier = " samer@1: + usaMultiplier samer@1: + "; maximum = " samer@1: + maximum); samer@1: } samer@1: samer@1: dataModel.setMaximum(maximum); samer@1: samer@1: if (resetCurrentValues) { samer@1: dataModel.setDoubleValue(maximum); samer@1: } samer@1: } samer@1: samer@1: private static void initLookAndFeel() { samer@1: String lookAndFeel = null; samer@1: samer@1: if (LOOKANDFEEL != null) { samer@1: if (LOOKANDFEEL.equals("Metal")) { samer@1: lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); samer@1: } else if (LOOKANDFEEL.equals("System")) { samer@1: lookAndFeel = UIManager.getSystemLookAndFeelClassName(); samer@1: } else if (LOOKANDFEEL.equals("Mac")) { samer@1: lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel"; samer@1: //PENDING: check! samer@1: } else if (LOOKANDFEEL.equals("Windows")) { samer@1: lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; samer@1: } else if (LOOKANDFEEL.equals("Motif")) { samer@1: lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; samer@1: } samer@1: samer@1: if (DEBUG) { samer@1: System.out.println("About to request look and feel: " samer@1: + lookAndFeel); samer@1: } samer@1: samer@1: try { samer@1: UIManager.setLookAndFeel(lookAndFeel); samer@1: } catch (ClassNotFoundException e) { samer@1: System.err.println("Couldn't find class for specified look and feel:" samer@1: + lookAndFeel); samer@1: System.err.println("Did you include the L&F library in the class path?"); samer@1: System.err.println("Using the default look and feel."); samer@1: } catch (UnsupportedLookAndFeelException e) { samer@1: System.err.println("Can't use the specified look and feel (" samer@1: + lookAndFeel samer@1: + ") on this platform."); samer@1: System.err.println("Using the default look and feel."); samer@1: } catch (Exception e) { samer@1: System.err.println("Couldn't get specified look and feel (" samer@1: + lookAndFeel samer@1: + "), for some reason."); samer@1: System.err.println("Using the default look and feel."); samer@1: e.printStackTrace(); samer@1: } samer@1: } samer@1: } samer@1: samer@1: public static void main(String[] args) { samer@1: initLookAndFeel(); samer@1: Converter converter = new Converter(); samer@1: samer@1: //Create a new window. samer@1: JFrame f = new JFrame("Converter"); samer@1: f.addWindowListener(new WindowAdapter() { samer@1: public void windowClosing(WindowEvent e) { samer@1: System.exit(0); samer@1: } samer@1: }); samer@1: samer@1: //Add the JPanel to the window and display the window. samer@1: //We can use a JPanel for the content pane because samer@1: //JPanel is opaque. samer@1: f.setContentPane(converter.mainPane); samer@1: if (COLORS) { samer@1: //This has no effect, since the JPanel completely samer@1: //covers the content pane. samer@1: f.getContentPane().setBackground(Color.green); samer@1: } samer@1: samer@1: f.pack(); //Resizes the window to its natural size. samer@1: f.setVisible(true); samer@1: } samer@1: }