// Program to calculate values of resistors import java.applet.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; // Main class begins here public class ResistorApplet extends JApplet implements ActionListener { JLabel picture; // Resistor Picture int numOfBands=0; // integer for number of bands (0=4-band, 1=5-band) int band1=0; // initialisation of band1 combo to 0 int band2=0; // initialisation of band2 combo to 0 int band3=0; // initialisation of band3 combo to 0 int band4=0; // initialisation of band4 combo to 0 int band5=0; // initialisation of band5 combo to 0 static float value=0; // value float for ohms static float kvalue; // value float for k ohms static float mvalue; // value float for M ohms float multiplier=0; // multiplier float for multiplier band static int tol=1; // tol interger for tolerance band static JTextField myTextField; // Text field for value static JTextField myTextField1; // Text field for kvalue static JTextField myTextField2; // Text field for mvalue static JTextField myTextField3; // Text field for tol JComboBox colourList1; // Combo box for colours JButton calcButton; // Button to calculate values // Image for four bands ImageIcon fourBandIcon = createAppletImageIcon("images/fourband.gif", "4-band"); // Image for five bands ImageIcon fiveBandIcon = createAppletImageIcon("images/fiveband.gif", "5-band"); aboutProgramWindow aboutWindow; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); c.setBackground(Color.white); // Combo box values for number of bands String[] bandNums = { "4-band", "5-band" }; // Combo box values for normal set of colours list String[] colours = { "black", "brown", "red", "orange", "yellow", "green", "blue", "purple", "white", "grey" }; // Combo box values for normal set of colours list + silver and gold String[] colours2 = { "black", "brown", "red", "orange", "yellow", "green", "blue", "gold", "silver" }; // Combo box values for tolerance set of colours list String[] colours3 = { "brown", "red", "gold", "silver", "none" }; // Program Title JTextField title = new JTextField("Resistor 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); // Create the band selector combo box, select the 4-band default JComboBox bandList = new JComboBox(bandNums); bandList.setSelectedIndex(0); bandList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) // When selected, do... { JComboBox cb = (JComboBox)e.getSource(); String bandNum = (String)cb.getSelectedItem(); // Set the bandNum String if (bandNum == "4-band") // If 4-band, select 4-band picture picture.setIcon(fourBandIcon); else // Else must be 5-band, select 5-band picture picture.setIcon(fiveBandIcon); // Set numOfBands int for calculations below numOfBands = (int)cb.getSelectedIndex(); } }); c.add(bandList); // Add the picture, default is fourband picture = new JLabel(fourBandIcon); picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0)); picture.setPreferredSize(new Dimension(320, 230+10)); c.add(picture); // Create the 1st combo box, select the black JComboBox colourList1 = new JComboBox(colours); colourList1.setSelectedIndex(0); colourList1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); band1 = (int)cb.getSelectedIndex(); } }); c.add(colourList1); // Create the 2nd combo box, select the black JComboBox colourList2 = new JComboBox(colours); colourList2.setSelectedIndex(0); colourList2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); band2 = (int)cb.getSelectedIndex(); } }); c.add(colourList2); // Create the 3rd combo box, select the black JComboBox colourList3 = new JComboBox(colours); colourList3.setSelectedIndex(0); colourList3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); band3 = (int)cb.getSelectedIndex(); } }); c.add(colourList3); // Create the 4th combo box, select the black JComboBox colourList4 = new JComboBox(colours2); colourList4.setSelectedIndex(0); colourList4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); band4 = (int)cb.getSelectedIndex(); } }); c.add(colourList4); // Create the 5th combo box, select the brown JComboBox colourList5 = new JComboBox(colours3); colourList5.setSelectedIndex(0); colourList5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); band5 = (int)cb.getSelectedIndex(); } }); c.add(colourList5); // Create the 1st text field, for value in ohms myTextField = new JTextField(" 0 Ohm"); 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 kilo ohms myTextField1 = new JTextField(" 0 k"); 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 mega ohms myTextField2 = new JTextField(" 0 M"); myTextField2.setFont(new Font("Courier", Font.PLAIN, 12)); myTextField2.setEditable(false); myTextField2.setBackground(Color.white); c.add(myTextField2); // Create the 4th text field, for tolerance value myTextField3 = new JTextField(" 0 %"); myTextField3.setFont(new Font("Courier", Font.PLAIN, 12)); myTextField3.setEditable(false); myTextField3.setBackground(Color.white); c.add(myTextField3); // 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(); } 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) { if (numOfBands == 0) // if 4-bands { value = 0; // make sure values are reset multiplier = 0; // make sure multiplier is reset switch (band3) // Switch to do the multiplications from band 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 value = (band1 * 10) + band2; value = (value * multiplier); kvalue = (value / 1000f); mvalue = (value / 1000000f); } else // Else 5-band resistor calculations { value = 0; // make sure values are reset multiplier = 0; // make sure multiplier is reset switch (band4) // Switch to do the multiplications from band 4 { 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; case 7: multiplier = 0.1f; break; case 8: multiplier = 0.01f; break; default: multiplier = 1; } // Do the maths value = (band1*100)+(band2*10)+(band3); value = ((float)value * (float)multiplier); kvalue = (value / 1000f); mvalue = (value / 1000000f); } switch (band5) // Switch to work out the tolerances from band 5 { case 0: tol = 1; break; case 1: tol = 2; break; case 2: tol = 5; break; case 3: tol = 10; break; case 4: tol = 20; break; default: tol = 20; } // Write the value to textfield StringBuffer sb = new StringBuffer(" "); sb.insert(15, (ResistorApplet.value+" Ohm")); ResistorApplet.myTextField.setText(sb.toString()); // Write the kvalue to textfield1 StringBuffer sb1 = new StringBuffer(" "); sb1.insert(15, (ResistorApplet.kvalue+" k")); ResistorApplet.myTextField1.setText(sb1.toString()); // Write the mvalue to textfield2 StringBuffer sb2 = new StringBuffer(" "); sb2.insert(15, (ResistorApplet.mvalue+" M")); ResistorApplet.myTextField2.setText(sb2.toString()); // Write the tolerance to textfield3 StringBuffer sb3 = new StringBuffer(" "); sb3.insert(3, (ResistorApplet.tol+" %")); ResistorApplet.myTextField3.setText(sb3.toString()); } } // PRE-WRITTEN - Handles the images... // http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html protected static ImageIcon createAppletImageIcon(String path, String description) { int MAX_IMAGE_SIZE = 4000; int count = 0; BufferedInputStream imgStream = new BufferedInputStream( ResistorApplet.class.getResourceAsStream(path)); if (imgStream != null) { byte buf[] = new byte[MAX_IMAGE_SIZE]; try { count = imgStream.read(buf); } catch (IOException ieo) { System.err.println("Couldn't read stream from file: " + path); } try { imgStream.close(); } catch (IOException ieo) { System.err.println("Can't close file " + path); } if (count <= 0) { System.err.println("Empty file: " + path); return null; } return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf), description); } else { System.err.println("Couldn't find file: " + path); return null; } } } 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(" Resistor 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); } }); } }