// Program to calculate values of capacitors import java.applet.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; // Main class begins here public class CapacitorApplet extends JApplet implements ActionListener { int digit1=0; // initialisation of digit1 combo to 0 int digit2=0; // initialisation of digit2 combo to 0 int digit3=0; // initialisation of digit3 combo to 0 static float pvalue=0; // value float for picofarads (pF) static float nvalue; // value float for nanofarads (nF) static float uvalue; // value float for microfarads (uF) float multiplier=0; // multiplier float for multiplier digit static JTextField myTextField; // Text field for value static JTextField myTextField1; // Text field for kvalue static JTextField myTextField2; // Text field for mvalue JButton calcButton; // Button to calculate values aboutProgramWindow aboutWindow; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.white); // Combo box values for number of bands String[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; String[] multDigits = { "0", "1", "2", "3", "4", "5", "6" }; // Program Title JTextField title = new JTextField("Capacitor Calculator"); title.setFont(new Font("Verdana", Font.BOLD, 32)); title.setEditable(false); title.setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); title.setBackground(Color.white); c.add(title); // Digits text JTextField digitText = new JTextField("Digits: "); digitText.setFont(new Font("Verdana", Font.BOLD, 14)); digitText.setEditable(false); digitText.setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); digitText.setBackground(Color.white); c.add(digitText); // Create the 1st combo box, select 0 JComboBox digitList1 = new JComboBox(digits); digitList1.setSelectedIndex(0); digitList1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); digit1 = (int)cb.getSelectedIndex(); } }); c.add(digitList1); // Create the 2nd combo box, select 0 JComboBox digitList2 = new JComboBox(digits); digitList2.setSelectedIndex(0); digitList2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); digit2 = (int)cb.getSelectedIndex(); } }); c.add(digitList2); // Create the 3rd combo box, select 0 JComboBox digitList3 = new JComboBox(multDigits); digitList3.setSelectedIndex(0); digitList3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); digit3 = (int)cb.getSelectedIndex(); } }); c.add(digitList3); // Button to perform calculations JButton myButton = new JButton("Calculate"); myButton.addActionListener(new calculateButtonHandler()); c.add(myButton); // About Button JButton aboutButton = new JButton("About"); aboutButton.addActionListener(this); c.add(aboutButton); aboutWindow = new aboutProgramWindow(getAppletContext()); aboutWindow.pack(); // Create the 1st text field, for value in pF myTextField = new JTextField(" 0 pF"); myTextField.setFont(new Font("Courier", Font.PLAIN, 12)); myTextField.setEditable(false); myTextField.setBackground(Color.white); c.add(myTextField); // Create the 2nd text field, for value in nF myTextField1 = new JTextField(" 0 nF"); myTextField1.setFont(new Font("Courier", Font.PLAIN, 12)); myTextField1.setEditable(false); myTextField1.setBackground(Color.white); c.add(myTextField1); // Create the 3rd text field, for value in uF myTextField2 = new JTextField(" 0 uF"); myTextField2.setFont(new Font("Courier", Font.PLAIN, 12)); myTextField2.setEditable(false); myTextField2.setBackground(Color.white); c.add(myTextField2); } public void destroy() { aboutWindow.setVisible(false); aboutWindow = null; } public void actionPerformed(ActionEvent event) { aboutWindow.setVisible(true); } // Performed when calculate button is pressed class calculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { pvalue = 0; // make sure values are reset multiplier = 0; // make sure multiplier is reset switch (digit3) // Switch to do the multiplications from digit 3 { case 0: multiplier = 1; break; case 1: multiplier = 10; break; case 2: multiplier = 100; break; case 3: multiplier = 1000; break; case 4: multiplier = 10000; break; case 5: multiplier = 100000; break; case 6: multiplier = 1000000; break; default: multiplier = 1; } // Do the maths pvalue = (digit1 * 10) + digit2; pvalue = (pvalue * multiplier); nvalue = (pvalue / 1000f); uvalue = (pvalue / 1000000f); // Write the pvalue to textfield StringBuffer sb = new StringBuffer(" "); sb.insert(14, (CapacitorApplet.pvalue+" pF")); CapacitorApplet.myTextField.setText(sb.toString()); // Write the nvalue to textfield1 StringBuffer sb1 = new StringBuffer(" "); sb1.insert(14, (CapacitorApplet.nvalue+" nF")); CapacitorApplet.myTextField1.setText(sb1.toString()); // Write the uvalue to textfield2 StringBuffer sb2 = new StringBuffer(" "); sb2.insert(14, (CapacitorApplet.uvalue+" uF")); CapacitorApplet.myTextField2.setText(sb2.toString()); } } } class aboutProgramWindow extends JFrame //implements ActionListener { TextField aboutField; AppletContext appletContext; public aboutProgramWindow(AppletContext appletContext) { super("About this Applet!"); this.appletContext = appletContext; Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.setBackground(Color.white); JLabel label1 = new JLabel(" Capacitor Calculator Applet Produced by Electro-Dan "); label1.setFont(new Font("Verdana", Font.ITALIC, 16)); label1.setAlignmentX(Component.CENTER_ALIGNMENT); contentPane.add(label1); JLabel label2 = new JLabel("Free to use and distribute but do not modify without consent"); label2.setFont(new Font("Verdana", Font.BOLD, 12)); label2.setAlignmentX(Component.CENTER_ALIGNMENT); contentPane.add(label2); JLabel label3 = new JLabel("http://jump.to/electro-dan"); label3.setFont(new Font("Verdana", Font.BOLD, 12)); label3.setAlignmentX(Component.CENTER_ALIGNMENT); contentPane.add(label3); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { setVisible(false); } }); } }