samer@1
|
1 /*
|
samer@1
|
2 * 1.1+Swing version.
|
samer@1
|
3 */
|
samer@1
|
4
|
samer@1
|
5 import javax.swing.*;
|
samer@1
|
6 import javax.swing.event.*;
|
samer@1
|
7 import java.awt.*;
|
samer@1
|
8 import java.awt.event.*;
|
samer@1
|
9 import java.util.*;
|
samer@1
|
10
|
samer@1
|
11 public class Converter {
|
samer@1
|
12 ConversionPanel metricPanel, usaPanel;
|
samer@1
|
13 Unit[] metricDistances = new Unit[3];
|
samer@1
|
14 Unit[] usaDistances = new Unit[4];
|
samer@1
|
15 final static boolean COLORS = false;
|
samer@1
|
16 final static boolean DEBUG = false;
|
samer@1
|
17 final static String LOOKANDFEEL = null;
|
samer@1
|
18 ConverterRangeModel dataModel = new ConverterRangeModel();
|
samer@1
|
19 JPanel mainPane;
|
samer@1
|
20
|
samer@1
|
21 /**
|
samer@1
|
22 * Create the ConversionPanels (one for metric, another for U.S.).
|
samer@1
|
23 * I used "U.S." because although Imperial and U.S. distance
|
samer@1
|
24 * measurements are the same, this program could be extended to
|
samer@1
|
25 * include volume measurements, which aren't the same.
|
samer@1
|
26 *
|
samer@1
|
27 * Put the ConversionPanels into a frame, and bring up the frame.
|
samer@1
|
28 */
|
samer@1
|
29 public Converter() {
|
samer@1
|
30 //Create Unit objects for metric distances, and then
|
samer@1
|
31 //instantiate a ConversionPanel with these Units.
|
samer@1
|
32 metricDistances[0] = new Unit("Centimeters", 0.01);
|
samer@1
|
33 metricDistances[1] = new Unit("Meters", 1.0);
|
samer@1
|
34 metricDistances[2] = new Unit("Kilometers", 1000.0);
|
samer@1
|
35 metricPanel = new ConversionPanel(this, "Metric System",
|
samer@1
|
36 metricDistances,
|
samer@1
|
37 dataModel);
|
samer@1
|
38
|
samer@1
|
39 //Create Unit objects for U.S. distances, and then
|
samer@1
|
40 //instantiate a ConversionPanel with these Units.
|
samer@1
|
41 usaDistances[0] = new Unit("Inches", 0.0254);
|
samer@1
|
42 usaDistances[1] = new Unit("Feet", 0.305);
|
samer@1
|
43 usaDistances[2] = new Unit("Yards", 0.914);
|
samer@1
|
44 usaDistances[3] = new Unit("Miles", 1613.0);
|
samer@1
|
45 usaPanel = new ConversionPanel(this, "U.S. System",
|
samer@1
|
46 usaDistances,
|
samer@1
|
47 new FollowerRangeModel(dataModel));
|
samer@1
|
48
|
samer@1
|
49 //Create a JPanel, and add the ConversionPanels to it.
|
samer@1
|
50 mainPane = new JPanel();
|
samer@1
|
51 if (COLORS) {
|
samer@1
|
52 mainPane.setBackground(Color.red);
|
samer@1
|
53 }
|
samer@1
|
54 mainPane.setLayout(new GridLayout(2,1,5,5));
|
samer@1
|
55 mainPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
samer@1
|
56 mainPane.add(metricPanel);
|
samer@1
|
57 mainPane.add(usaPanel);
|
samer@1
|
58 resetMaxValues(true);
|
samer@1
|
59 }
|
samer@1
|
60
|
samer@1
|
61 public void resetMaxValues(boolean resetCurrentValues) {
|
samer@1
|
62 double metricMultiplier = metricPanel.getMultiplier();
|
samer@1
|
63 double usaMultiplier = usaPanel.getMultiplier();
|
samer@1
|
64 int maximum = ConversionPanel.MAX;
|
samer@1
|
65
|
samer@1
|
66 if (metricMultiplier > usaMultiplier) {
|
samer@1
|
67 maximum = (int)(ConversionPanel.MAX *
|
samer@1
|
68 (usaMultiplier/metricMultiplier));
|
samer@1
|
69 }
|
samer@1
|
70
|
samer@1
|
71 if (DEBUG) {
|
samer@1
|
72 System.out.println("in Converter resetMaxValues");
|
samer@1
|
73 System.out.println(" metricMultiplier = "
|
samer@1
|
74 + metricMultiplier
|
samer@1
|
75 + "; usaMultiplier = "
|
samer@1
|
76 + usaMultiplier
|
samer@1
|
77 + "; maximum = "
|
samer@1
|
78 + maximum);
|
samer@1
|
79 }
|
samer@1
|
80
|
samer@1
|
81 dataModel.setMaximum(maximum);
|
samer@1
|
82
|
samer@1
|
83 if (resetCurrentValues) {
|
samer@1
|
84 dataModel.setDoubleValue(maximum);
|
samer@1
|
85 }
|
samer@1
|
86 }
|
samer@1
|
87
|
samer@1
|
88 private static void initLookAndFeel() {
|
samer@1
|
89 String lookAndFeel = null;
|
samer@1
|
90
|
samer@1
|
91 if (LOOKANDFEEL != null) {
|
samer@1
|
92 if (LOOKANDFEEL.equals("Metal")) {
|
samer@1
|
93 lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
|
samer@1
|
94 } else if (LOOKANDFEEL.equals("System")) {
|
samer@1
|
95 lookAndFeel = UIManager.getSystemLookAndFeelClassName();
|
samer@1
|
96 } else if (LOOKANDFEEL.equals("Mac")) {
|
samer@1
|
97 lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
|
samer@1
|
98 //PENDING: check!
|
samer@1
|
99 } else if (LOOKANDFEEL.equals("Windows")) {
|
samer@1
|
100 lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
|
samer@1
|
101 } else if (LOOKANDFEEL.equals("Motif")) {
|
samer@1
|
102 lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
|
samer@1
|
103 }
|
samer@1
|
104
|
samer@1
|
105 if (DEBUG) {
|
samer@1
|
106 System.out.println("About to request look and feel: "
|
samer@1
|
107 + lookAndFeel);
|
samer@1
|
108 }
|
samer@1
|
109
|
samer@1
|
110 try {
|
samer@1
|
111 UIManager.setLookAndFeel(lookAndFeel);
|
samer@1
|
112 } catch (ClassNotFoundException e) {
|
samer@1
|
113 System.err.println("Couldn't find class for specified look and feel:"
|
samer@1
|
114 + lookAndFeel);
|
samer@1
|
115 System.err.println("Did you include the L&F library in the class path?");
|
samer@1
|
116 System.err.println("Using the default look and feel.");
|
samer@1
|
117 } catch (UnsupportedLookAndFeelException e) {
|
samer@1
|
118 System.err.println("Can't use the specified look and feel ("
|
samer@1
|
119 + lookAndFeel
|
samer@1
|
120 + ") on this platform.");
|
samer@1
|
121 System.err.println("Using the default look and feel.");
|
samer@1
|
122 } catch (Exception e) {
|
samer@1
|
123 System.err.println("Couldn't get specified look and feel ("
|
samer@1
|
124 + lookAndFeel
|
samer@1
|
125 + "), for some reason.");
|
samer@1
|
126 System.err.println("Using the default look and feel.");
|
samer@1
|
127 e.printStackTrace();
|
samer@1
|
128 }
|
samer@1
|
129 }
|
samer@1
|
130 }
|
samer@1
|
131
|
samer@1
|
132 public static void main(String[] args) {
|
samer@1
|
133 initLookAndFeel();
|
samer@1
|
134 Converter converter = new Converter();
|
samer@1
|
135
|
samer@1
|
136 //Create a new window.
|
samer@1
|
137 JFrame f = new JFrame("Converter");
|
samer@1
|
138 f.addWindowListener(new WindowAdapter() {
|
samer@1
|
139 public void windowClosing(WindowEvent e) {
|
samer@1
|
140 System.exit(0);
|
samer@1
|
141 }
|
samer@1
|
142 });
|
samer@1
|
143
|
samer@1
|
144 //Add the JPanel to the window and display the window.
|
samer@1
|
145 //We can use a JPanel for the content pane because
|
samer@1
|
146 //JPanel is opaque.
|
samer@1
|
147 f.setContentPane(converter.mainPane);
|
samer@1
|
148 if (COLORS) {
|
samer@1
|
149 //This has no effect, since the JPanel completely
|
samer@1
|
150 //covers the content pane.
|
samer@1
|
151 f.getContentPane().setBackground(Color.green);
|
samer@1
|
152 }
|
samer@1
|
153
|
samer@1
|
154 f.pack(); //Resizes the window to its natural size.
|
samer@1
|
155 f.setVisible(true);
|
samer@1
|
156 }
|
samer@1
|
157 }
|