/*This is a small GUI application that I made for personal use.  It
allows you to enter the colors of the bands on a 4-band fixed resistor
to calculate the resistance (and tolerance) of that particular resistor.
Eventually, I will create a version that deals with 5-band and 6-band
resistors, but 4-band will do for now. There is a band which is gold,
silver or no color (this represents the tolerance/precision). This band
I term as band D, and the color band next to that is band C, followed by
band B, and then band A.  The format is different in 5-band and 6-band
resistors, but this application deals exclusively with 4-band resistors.*/



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;


public class OhmCalc extends JPanel
{
    JPanel entryPanel;//Panel containing textboxes for entering band colors
    JTextField bandA;//Textfield for entering the first band color
    JTextField bandB;//Textfield for entering the second band color
    JTextField bandC;//Textfield for entering the third band color
    JTextField bandD;//Textfield for entering the fourth (tolerance) band color
    JTextArea result;//Text area that displays the resistance value of the resistor
    
    //Labels for the textfields
    JLabel bandALabel;
    JLabel bandBLabel;
    JLabel bandCLabel;
    JLabel bandDLabel;
    JLabel resultLabel;
    
    JButton submit;//button to submit band colors to obtain resistance
    JButton clear;//button to clear the textfields
        
    public OhmCalc()
    {
        entryPanel = new JPanel();
        bandA = new JTextField(10);
        bandB = new JTextField(10);
        bandC = new JTextField(10);
        bandD = new JTextField(10);
        result = new JTextArea(40,40);
        resultLabel = new JLabel("Resistance (in ohms): ");
        bandALabel = new JLabel("Band A ");
        bandBLabel = new JLabel("Band B ");
        bandCLabel = new JLabel("Band C ");
        bandDLabel = new JLabel("Band D ");
        submit = new JButton("Submit");
        clear = new JButton("Clear");
        
        setPreferredSize(new Dimension(400,400));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        entryPanel.setPreferredSize(new Dimension(100,200));
        entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.Y_AXIS));
        add(entryPanel);

        entryPanel.add(bandALabel);
        entryPanel.add(bandA);

        entryPanel.add(bandBLabel);
        entryPanel.add(bandB);

        entryPanel.add(bandCLabel);
        entryPanel.add(bandC);

        entryPanel.add(bandDLabel);
        entryPanel.add(bandD);

        add(Box.createRigidArea(new Dimension(0,10))); //creates a space between components
        add(resultLabel);
        add(result);
        add(Box.createRigidArea(new Dimension(0,10)));
        add(submit);
        add(Box.createRigidArea(new Dimension(0,10)));
        add(clear);
        
        
        submit.addActionListener(new SubmitListener());
        clear.addActionListener(new ClearListener());
    }
    
    private class SubmitListener implements ActionListener
    {
        //Returns the digit value of the input color in bands A, B or C
        public int r(String band)
        {
            if(band.equals("black"))
            {
                return 0;
            }
            else if(band.equals("brown"))
            {
                return 1;
            }
            else if(band.equals("red"))
            {
                return 2;
            }
            else if(band.equals("orange"))
            {
                return 3;
            }
            else if(band.equals("yellow"))
            {
                return 4;
            }
            else if(band.equals("green"))
            {
                return 5;
            }
            else if(band.equals("blue"))
            {
                return 6;
            }
            else if(band.equals("violet") || band.equals("purple"))
            {
                return 7;
            }
            else if(band.equals("grey") || band.equals("gray"))
            {
                return 8;
            }
            else if(band.equals("white"))
            {
                return 9;
            }
            else
            {
                return -1;
            }
        }
        
        //Used to check the tolerance band (band D) text input for error
        public int tolErrorRec(String band)
        {
            if(band.equals("gold"))
            {
                return 0;
            }
            else if(band.equals("silver"))
            {
                return 1;
            }
            else if(band.equals("") || band.equals("no color"))
            {
                return 2;
            }
            else{
                return -1;
            }
        }
        
        public void actionPerformed(ActionEvent e)
        {
            try{
                String bA = bandA.getText();
                String bB = bandB.getText();
                String bC = bandC.getText();
                String bD = bandD.getText();
                
                //Checks the input text for error
                if((r(bA) == -1) || (r(bB) == -1) || (r(bC) == -1) || (tolErrorRec(bD) == -1))
                
                {
                    JOptionPane.showMessageDialog(null, "There is an error in the text.");
                }
                else{
                    double rC = Math.pow(10, r(bC)); //obtains the exponent of the resistance value
                    double resistance = ((r(bA)*10)+r(bB))*rC; //calculates the resistance value
                    
                    int tolerance = 0;
                    if(bD.equals("gold"))
                    {
                        tolerance += 5;
                    }
                    else if(bD.equals("silver"))
                    {
                        tolerance += 10;
                    }
                    else if(bD.equals("") || bD.equals("no color"))
                    {
                        tolerance += 20;
                    }
             
                
                    result.setText("Resistance = "+resistance+" ohms");
                    result.setText(result.getText()+"\nTolerance = +/- "+tolerance+"%");
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "There is an error in the text.");
            }
        }
    }
    
    //Clears the text-fields and text area 
    private class ClearListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            bandA.setText("");
            bandB.setText("");
            bandC.setText("");
            bandD.setText("");
            result.setText("");
        }
    }
    
    //Creates the window
    public static void main(String[] args)
    {
       JFrame frame = new JFrame ("Resistor Calculator");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

       frame.getContentPane().add(new OhmCalc());

       frame.pack();
       frame.setVisible(true);
    }
}

An idea for an improvement, use a drop down list to chose the colors instead of requiring the user to type them in.

I made one for physics class, except mine gives out values and then you need to add the colors onto the resistor. download it here: http://www.mediafire.com/?6r9pzn4y1wdmd14

I left out the tolerance on mine

That is the whole library (at least as much as we got done) that I uploaded.

Give me some feedback.

Instructions:
1. Go to the electricity tab, then clock click the program under the tab.
2. Click launch (in the yellow)

To use the Program:
1. select the ring from the top
2. Select the color for that ring
3. Click add ring (at the top)
4. Repeat for other rings.
5. Once done, click check under (under the colors)
6. If correct click new Number (or something like that) to generate a new number, if incorrect the click OK and try again.
7. To close the program, you guessed it. Click the X in the corner.

Edited 5 Years Ago by sirlink99: n/a

Thanks for your feedback, NormR1. I took your advice, and replaced with the textfields with drop-down lists:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;


public class OhmCalc extends JPanel
{
    JPanel entryPanel;//Panel containing textboxes for entering band colors
    String[] colors = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
    String[] tolerances = {"gold", "silver", "colorless"};
    
    JComboBox bandAList = new JComboBox(colors); //Drop-down list of possible colors for band A
    JComboBox bandBList = new JComboBox(colors); //Drop-down list of possible colors for band B
    JComboBox bandCList = new JComboBox(colors); //Drop-down list of possible colors for band C
    JComboBox bandDList = new JComboBox(tolerances); //Drop-down list of possible colors for tolerance band
    
    JTextArea result;//Text area that displays the resistance value of the resistor
    
    //Labels for the textfields
    JLabel bandALabel;
    JLabel bandBLabel;
    JLabel bandCLabel;
    JLabel bandDLabel;
    JLabel resultLabel;
    
    JButton submit;//button to submit band colors to obtain resistance
    JButton clear;//button to clear the textfields
        
    public OhmCalc()
    {
        entryPanel = new JPanel();
        result = new JTextArea(40,40);
        resultLabel = new JLabel("Resistance (in ohms): ");
        bandALabel = new JLabel("Band A ");
        bandBLabel = new JLabel("Band B ");
        bandCLabel = new JLabel("Band C ");
        bandDLabel = new JLabel("Band D (tolerance) ");
        submit = new JButton("Submit");
        clear = new JButton("Clear");
        
        setPreferredSize(new Dimension(400,400));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        entryPanel.setPreferredSize(new Dimension(100,200));
        entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.Y_AXIS));
        add(entryPanel);
        
        entryPanel.add(bandALabel);
        entryPanel.add(bandAList);
        add(Box.createRigidArea(new Dimension(0,10)));
        entryPanel.add(bandBLabel);
        entryPanel.add(bandBList);
        add(Box.createRigidArea(new Dimension(0,10)));
        entryPanel.add(bandCLabel);
        entryPanel.add(bandCList);
        add(Box.createRigidArea(new Dimension(0,10)));
        entryPanel.add(bandDLabel);
        entryPanel.add(bandDList);
        
        add(Box.createRigidArea(new Dimension(0,10)));
        add(resultLabel);
        add(result);
        add(Box.createRigidArea(new Dimension(0,10)));
        add(submit);
        add(Box.createRigidArea(new Dimension(0,10)));
        add(clear);
        
        
        submit.addActionListener(new SubmitListener());
        clear.addActionListener(new ClearListener());
    }
    
    private class SubmitListener implements ActionListener
    {
        //Returns the digit value of the band color
        public int r(String band)
        {
            if(band.equals("black"))
            {
                return 0;
            }
            else if(band.equals("brown"))
            {
                return 1;
            }
            else if(band.equals("red"))
            {
                return 2;
            }
            else if(band.equals("orange"))
            {
                return 3;
            }
            else if(band.equals("yellow"))
            {
                return 4;
            }
            else if(band.equals("green"))
            {
                return 5;
            }
            else if(band.equals("blue"))
            {
                return 6;
            }
            else if(band.equals("violet") || band.equals("purple"))
            {
                return 7;
            }
            else if(band.equals("grey") || band.equals("gray"))
            {
                return 8;
            }
            else if(band.equals("white"))
            {
                return 9;
            }
            else
            {
                return -1;
            }
        }
        
        public int tolErrorRec(String band)
        //Used to check the tolerance band text input for error
        {
            if(band.equals("gold"))
            {
                return 0;
            }
            else if(band.equals("silver"))
            {
                return 1;
            }
            else if(band.equals("colorless"))
            {
                return 2;
            }
            else{
                return -1;
            }
        }
        
        public void actionPerformed(ActionEvent e)
        {
            try{
                String bA = (String)bandAList.getSelectedItem();
                String bB = (String)bandBList.getSelectedItem();
                String bC = (String)bandCList.getSelectedItem();
                String bD = (String)bandDList.getSelectedItem();
                
                if((r(bA) == -1) || (r(bB) == -1) || (r(bC) == -1) || (tolErrorRec(bD) == -1))
                //Checks the input text for error
                {
                    JOptionPane.showMessageDialog(null, "There is an error in the text.");
                }
                else{
                    double rC = Math.pow(10, r(bC)); //obtains the exponent of the resistance value
                    double resistance = ((r(bA)*10)+r(bB))*rC; //calculates the resistance value
                    
                    int tolerance = 0;
                    if(bD.equals("gold"))
                    {
                        tolerance += 5;
                    }
                    else if(bD.equals("silver"))
                    {
                        tolerance += 10;
                    }
                    else if(bD.equals("colorless"))
                    {
                        tolerance += 20;
                    }
             
                
                    result.setText("Resistance = "+resistance+" ohms");
                    result.setText(result.getText()+"\nTolerance = +/- "+tolerance+"%");
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "There is an error in the text.");
            }
        }
    }
    
    
    private class ClearListener implements ActionListener
    {
        //Clears the result text area
        public void actionPerformed(ActionEvent e)
        {
           result.setText("");
        }
    }
    
    public static void main(String[] args)
    {
       JFrame frame = new JFrame ("Resistor Calculator");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

       frame.getContentPane().add(new OhmCalc());

       frame.pack();
       frame.setVisible(true);
    }
}

I made one for physics class, except mine gives out values and then you need to add the colors onto the resistor. download it here: http://www.mediafire.com/?6r9pzn4y1wdmd14

I left out the tolerance on mine

That is the whole library (at least as much as we got done) that I uploaded.

Give me some feedback.

Instructions:
1. Go to the electricity tab, then clock click the program under the tab.
2. Click launch (in the yellow)

To use the Program:
1. select the ring from the top
2. Select the color for that ring
3. Click add ring (at the top)
4. Repeat for other rings.
5. Once done, click check under (under the colors)
6. If correct click new Number (or something like that) to generate a new number, if incorrect the click OK and try again.
7. To close the program, you guessed it. Click the X in the corner.

sirlink, I like your app, and it definitely looks like something that a physics/electronics/EE student could use; in my opinion, all it probably needs (besides being finished) is some documentation, which I know you'll add eventually, though, if there's any way you can make it a little more user-friendly (what I mean is, if there's any way to make it in such a way that it can be used intuitively instead of having to consult the documentation), that would be good. It'd be interesting to see the finished product.

@jamd200 are you planing to set correct colorizations for resistor category, consuptions in Watt and tolerance too,

@jamd200 are you planing to set correct colorizations for resistor category, consuptions in Watt and tolerance too,

If you mean, am I planning on including the correct color coding for different types of resistors, the power ratings, et al, I'm working on it. I'm still a student, so there are certain things I still need to learn myself.

Below is an "upgraded" version of the application, and it now allows you to enter the power rating too. I also added the colors I was missing in the last version to the multiplier and tolerance bands.

/*In this version, the resistor band colors were updated, and the app now allows one to enter
the power rating of the resistor to obtain its maximum allowed voltage and current.*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;


public class OhmCalc extends JPanel
{
    JPanel entryPanel;//Panel containing textboxes for entering band colors
    JPanel resultPanel;//Panel showing the calculated results
    String[] sigFigs = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
    String[] multipliers = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white", "gold", "silver"};
    String[] tolerances = {"brown", "red", "green", "blue", "violet", "gold", "silver", "colorless"};
    
    JComboBox bandAList = new JComboBox(sigFigs); //Drop-down list of possible colors for band A
    JComboBox bandBList = new JComboBox(sigFigs); //Drop-down list of possible colors for band B
    JComboBox bandCList = new JComboBox(multipliers); //Drop-down list of possible colors for band C
    JComboBox bandDList = new JComboBox(tolerances); //Drop-down list of possible colors for band A
    

    JTextField result;//Text field that displays the resistance value of the resistor
    JTextField toleranceField; //Text field that displays the tolerance of the resistor
    JTextField powerRatingField; //Text field for entering power rating
    JTextArea maxCVField; //Text area displaying the maximum voltage and current allowed through the resistor
    
    //Labels for the textfields
    JLabel bandALabel;
    JLabel bandBLabel;
    JLabel bandCLabel;
    JLabel bandDLabel;
    JLabel resultLabel;
    JLabel toleranceLabel;
    JLabel powerRatingLabel;
    JLabel maxCVLabel;
    
    JButton submit;//button to submit band colors to obtain resistance
    JButton submitPower;//button to submit power rating to obtain max allowed current/voltage
    JButton clear;//button to clear the textfields
        
    public OhmCalc()
    {
        entryPanel = new JPanel();
        resultPanel = new JPanel();
        result = new JTextField(10);
        toleranceField = new JTextField(10);
        powerRatingField = new JTextField(10);
        powerRatingField.setText("0");
        maxCVField = new JTextArea(20,20);
        resultLabel = new JLabel("Resistance (in ohms): ");
        bandALabel = new JLabel("Band A ");
        bandBLabel = new JLabel("Band B ");
        bandCLabel = new JLabel("Band C ");
        bandDLabel = new JLabel("Band D (Tolerance)");
        resultLabel = new JLabel("Resistance (in ohms):");
        toleranceLabel = new JLabel("Tolerance:");
        powerRatingLabel = new JLabel("Enter power rating here (optional): ");
        maxCVLabel = new JLabel("Maximum current and voltage: ");
        submit = new JButton("Submit band colors");
        submitPower = new JButton("Submit power rating");
        clear = new JButton("Clear");
        
        setPreferredSize(new Dimension(400,600));
        setLayout(new GridLayout(2,1));
        entryPanel.setPreferredSize(new Dimension(100,200));
        entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.Y_AXIS));
        resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.Y_AXIS));
        
        add(entryPanel);
        
        entryPanel.add(bandALabel);
        entryPanel.add(bandAList);
        entryPanel.add(Box.createRigidArea(new Dimension(0,5))); //Creates a space between the components
        entryPanel.add(bandBLabel);
        entryPanel.add(bandBList);
        entryPanel.add(Box.createRigidArea(new Dimension(0,5)));
        entryPanel.add(bandCLabel);
        entryPanel.add(bandCList);
        entryPanel.add(Box.createRigidArea(new Dimension(0,5)));
        entryPanel.add(bandDLabel);
        entryPanel.add(bandDList);
        entryPanel.add(Box.createRigidArea(new Dimension(0,5)));
        
        
        add(resultPanel);
        
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(resultLabel);
        resultPanel.add(result);
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(toleranceLabel);
        resultPanel.add(toleranceField);
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(submit);
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(powerRatingLabel);
        resultPanel.add(powerRatingField);
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(maxCVLabel);
        resultPanel.add(maxCVField);
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(submitPower);
        resultPanel.add(Box.createRigidArea(new Dimension(0,10)));
        resultPanel.add(clear);
        
        
        
        submit.addActionListener(new SubmitListener());
        submitPower.addActionListener(new SubmitPowerListener());
        clear.addActionListener(new ClearListener());
    }
    
    private class SubmitListener implements ActionListener
    {
        //Returns the digit value of the band color (applies to significant figures)
        public int sig(String band)
        {
            if(band.equals("black"))
            {
                return 0;
            }
            else if(band.equals("brown"))
            {
                return 1;
            }
            else if(band.equals("red"))
            {
                return 2;
            }
            else if(band.equals("orange"))
            {
                return 3;
            }
            else if(band.equals("yellow"))
            {
                return 4;
            }
            else if(band.equals("green"))
            {
                return 5;
            }
            else if(band.equals("blue"))
            {
                return 6;
            }
            else if(band.equals("violet"))
            {
                return 7;
            }
            else if(band.equals("gray"))
            {
                return 8;
            }
            else if(band.equals("white"))
            {
                return 9;
            }
            else
            {
                return -1;
            }
        }
        
        //Returns the digit value of the multiplier band color (band C)
        public int multi(String band)
        {
            if(band.equals("black"))
            {
                return 0;
            }
            else if(band.equals("brown"))
            {
                return 1;
            }
            else if(band.equals("red"))
            {
                return 2;
            }
            else if(band.equals("orange"))
            {
                return 3;
            }
            else if(band.equals("yellow"))
            {
                return 4;
            }
            else if(band.equals("green"))
            {
                return 5;
            }
            else if(band.equals("blue"))
            {
                return 6;
            }
            else if(band.equals("violet"))
            {
                return 7;
            }
            else if(band.equals("gray"))
            {
                return 8;
            }
            else if(band.equals("white"))
            {
                return 9;
            }
            else if(band.equals("gold"))
            {
                return -1;
            }
            else if(band.equals("silver"))
            {
                return -2;
            }
            else
            {
                return -3;
            }
        }
        
        public int tolErrorRec(String band)
        //Used to check the tolerance band text input for error
        {
            if(band.equals("gold") || band.equals("silver"))
            {
                return 0;
            }
            else if(band.equals("colorless"))
            {
                return 1;
            }
            else if(band.equals("brown") || band.equals("red"))
            {
                return 2;
            }
            else if(band.equals("green"))
            {
                return 3;
            }
            else if(band.equals("blue") || band.equals("violet"))
            {
                return 4;
            }
            else{
                return -1;
            }
        }
        
        public void actionPerformed(ActionEvent e)
        {
            try{
                String bA = (String)bandAList.getSelectedItem();
                String bB = (String)bandBList.getSelectedItem();
                String bC = (String)bandCList.getSelectedItem();
                String bD = (String)bandDList.getSelectedItem();
                
                
                if((sig(bA) == -1) || (sig(bB) == -1) || (multi(bC) == -3) || (tolErrorRec(bD) == -1))
                //Checks the input text for error
                {
                    JOptionPane.showMessageDialog(null, "There is an error in the resistance band input.");
                }
                else{
                    double rC = Math.pow(10, multi(bC)); //obtains the exponent of the resistance value
                    double resistance = ((sig(bA)*10)+sig(bB))*rC; //calculates the resistance value
                    
                    double tolerance = 0;
                    if(bD.equals("gold"))
                    {
                        tolerance += 5;
                    }
                    else if(bD.equals("silver"))
                    {
                        tolerance += 10;
                    }
                    else if(bD.equals("colorless"))
                    {
                        tolerance += 20;
                    }
                    else if(bD.equals("brown"))
                    {
                        tolerance += 1;
                    }
                    else if(bD.equals("red"))
                    {
                        tolerance += 2;
                    }
                    else if(bD.equals("green"))
                    {
                        tolerance += 0.5;
                    }
                    else if(bD.equals("blue"))
                    {
                        tolerance += 0.25;
                    }
                    else if(bD.equals("violet"))
                    {
                        tolerance += 0.1;
                    }
                    
                    result.setText(""+resistance);
                    toleranceField.setText("+/- "+tolerance+"%");
                    
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "There is an error in the text.");
            }
        }
    }
    
    private class SubmitPowerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            try{
                if(result.getText().equals(""))
                {
                    JOptionPane.showMessageDialog(null, "There needs to be a resistance in the resistance text-field.");
                }
                else{
                    double resistance = Double.parseDouble(result.getText()); //resistance of the resistor
                    double watt = Double.parseDouble(powerRatingField.getText()); //power rating of resistor
                
                    if(watt < 0)
                    {
                        JOptionPane.showMessageDialog(null, "The power rating cannot be negative.");
                    }
                    else{
                    
                        double maxVolt = Math.sqrt(watt*resistance); //maximum allowed voltage
                        double maxCurrent = Math.sqrt(watt/resistance); //maximum allowed current
                        maxCVField.setText("Maximum voltage possible: "+maxVolt+" V"+"\nMaximum current possible: "+maxCurrent+" A");
                    }
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "The power rating/resistance is not a recognized number.");
            }
        }
    }
    
    private class ClearListener implements ActionListener
    {
        //Clears the result text area
        public void actionPerformed(ActionEvent e)
        {
           result.setText("");
           toleranceField.setText("");
           powerRatingField.setText("0");
           maxCVField.setText("");
        }
    }
    
    public static void main(String[] args)
    {
       JFrame frame = new JFrame ("Resistor Calculator");
       frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

       frame.getContentPane().add(new OhmCalc());

       frame.pack();
       frame.setVisible(true);
    }
}
This question has already been answered. Start a new discussion instead.