Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get RGB codes of color?

843807Aug 25 2008 — edited Sep 1 2008
I am in the middle of developing a small program that allows the user to select a color from a list and then it will display an example of the color along with the RGB codes. I have the program working fine but I am looking for a more sufficient way. Does anyone know how to get the RGB codes from the color name (maybe the getColorComponet() method?) so I can get rid of having to type in the colors codes? If you do, can you please show me how because I am a complete noob to this and I love it but I need some help. Thanks guys!

Here is my code:
//ColorSelector.java


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


class ColorSelector extends JFrame implements ListSelectionListener {
        //Containers
        JPanel mainPanel;
        JPanel innerPanel;
        JPanel northPanel;
        //Components
 JLabel instructions; 
 JTextField colorCode;   //displays RGB codes of color
        String[] colorChoices = {"Red",  //color names in list
    "Blue",
    "Green",
    "Black",
    "Cyan",
    "Magenta",
    "Gray",
    "Light Gray",
    "Dark Gray",
    "Orange",
    "Pink",
    "White",
    "Yellow"};


 public ColorSelector(){
  setTitle("Grant Vinson's Color Selector");
  setSize(400,350);


  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });


  instructions = new JLabel("Select your favorite color");
  colorCode = new JTextField("Please select a color");


  JList colors = new JList(colorChoices);
  colors.addListSelectionListener(this);


  innerPanel = new JPanel();
  innerPanel.add(colors);
  innerPanel.setBackground(Color.white);


  northPanel = new JPanel();
  northPanel.setBackground(Color.white);
  northPanel.add(instructions);


  mainPanel = new JPanel();
  mainPanel.setLayout(new BorderLayout());
  mainPanel.setBackground(Color.white);


  mainPanel.add(northPanel,"North");
  mainPanel.add(innerPanel,"West");
  mainPanel.add(colorCode,"South");
  getContentPane().add(mainPanel);
 }


 public void valueChanged(ListSelectionEvent e) {
  JList source = (JList)e.getSource();

  Object[] values = source.getSelectedValues();


  for(int x=0;x<values.length;x++)
  {
                    String chosenColor = (String)values[x];

                    if (chosenColor == "Red")
                    {
   colorCode.setText("RGB CODES: R:255  G:0  B:0");
   mainPanel.setBackground(Color.red);
                    }
                    else if (chosenColor == "Blue")
                    {
   colorCode.setText("RGB CODES: R:0  G:0  B:255");
   mainPanel.setBackground(Color.blue);
                    }
                    else if (chosenColor == "Black")
                    {
                    colorCode.setText("RGB CODES: R:0  G:0  B:0");
                    mainPanel.setBackground(Color.black);
                    }
                    else if (chosenColor == "Orange")
                    {
   colorCode.setText("RGB CODES: R:255  G:165  B:0");
   mainPanel.setBackground(Color.orange);
                    }
                    else if (chosenColor == "Pink")
                    {
   colorCode.setText("RGB CODES: R:255  G:192  B:203");
   mainPanel.setBackground(Color.pink);
                    }
                    else if (chosenColor == "Yellow")
                    {
              colorCode.setText("RGB CODES: R:255  G:255  B:0");
   mainPanel.setBackground(Color.yellow);
                    }
                    else if (chosenColor == "White")
                    {
   colorCode.setText("RGB CODES: R:255  G:255  B:255");
   mainPanel.setBackground(Color.white);
                    }
                    else if (chosenColor == "Magenta")
                    {
   colorCode.setText("RGB CODES: R:255  G:0  B:255");
                           mainPanel.setBackground(Color.magenta);
                    }
                    else if (chosenColor == "Cyan")
                    {
   colorCode.setText("RGB CODES: R:0  G:255  B:255");
   mainPanel.setBackground(Color.cyan);
                    }
                    else if (chosenColor == "Gray")
                    {
   colorCode.setText("RGB CODES: R:128  G:128  B:128");
   mainPanel.setBackground(Color.gray);
                    }
                    else if (chosenColor == "Light Gray")
                    {
                           colorCode.setText("RGB CODES: R:211  G:211  B:211");
                           mainPanel.setBackground(Color.lightGray);
                    }
                    else if (chosenColor == "Dark Gray")
                    {
   colorCode.setText("RGB CODES: R:169  G:469  B:169");
   mainPanel.setBackground(Color.darkGray);
                    }
                    else if (chosenColor == "Green")
                    {
   colorCode.setText("RGB CODES: R:0  G:128  B:0");
   mainPanel.setBackground(Color.green);
                    }

  }//end for
 }//end of valueChanged()


 public static void main(String[] args) {
            boolean show = true;
            JFrame frame = new ColorSelector();
            frame.setVisible(show);
 }
}//end of ColorSelector class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 29 2008
Added on Aug 25 2008
11 comments
924 views