How do you to take user input for red, green and blue(a number between 0 and 255) and make the background of the center JPanel that color.

Heres the code on what ive done so far:

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

/**
 * Write a description of class ColorTestApplet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ColorTestApplet extends JApplet
{
    public void init()
    {
        this.setLayout(new BorderLayout());
        
        //Declaring panels
        centerPanel = new JPanel();
        northPanel = new JPanel();
        southPanel = new JPanel();
        
        //centerPanel.setLayout(new GridLayout());
        this.add(centerPanel, BorderLayout.CENTER);
        this.add(northPanel, BorderLayout.NORTH);
        this.add(southPanel, BorderLayout.SOUTH);
        
        JLabel red = new JLabel("Red:");
        JLabel green = new JLabel("Green:");
        JLabel blue = new JLabel("Blue:");
        
        redText = new JTextField("0", 5);
        greenText = new JTextField("0", 5);
        blueText = new JTextField("0", 5);
        redText.setHorizontalAlignment(redText.RIGHT);
        greenText.setHorizontalAlignment(redText.RIGHT);
        blueText.setHorizontalAlignment(redText.RIGHT);
        
        JButton show = new JButton("Show");
        
        JLabel name = new JLabel("Kimberlie Davis");
        
        northPanel.add(red);
        northPanel.add(redText);
        northPanel.add(green);
        northPanel.add(greenText);
        northPanel.add(blue);
        northPanel.add(blueText);
        southPanel.add(show);
        centerPanel.add(name);
        
        ShowAction showAction = new ShowAction();
        show.addActionListener(showAction);
    }
    
 class ShowAction implements ActionListener
 {
     public void actionPerformed(ActionEvent e)
     {
         int red, green, blue; 
         String color;
         try
         {
         red = Integer.parseInt(redText.getText());
         green = Integer.parseInt(greenText.getText());
         blue = Integer.parseInt(blueText.getText());
         if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0)
        {
          JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!");
          redText.setText("0");
          blueText.setText("0");
          greenText.setText("0");
        }
         else
         {
          color = "" + red + green + blue;
          JOptionPane.showMessageDialog(null, color);
          //centerPanel.setBackground(red, blue, green);
         }
         //centerpanel.setBackground(Color.color);
        }
        catch(NumberFormatException exception)
        {
         JOptionPane.showMessageDialog(null, "Please enter a number!"); 
         redText.setText("0");
         blueText.setText("0");
         greenText.setText("0");
        }
     }
  }
  
  JPanel centerPanel;
  JPanel northPanel;
  JPanel southPanel;
  
  JTextField redText;
  JTextField greenText;
  JTextField blueText;
}

Do you have to do it that way? I think a much more interesting way would be to use ColorChooser

I do have to have the user input numbers for red green and blue. Its for a school assignment.

Just create a new color using your values. I added the line of code into your try statement.

try
         {
         red = Integer.parseInt(redText.getText());
         green = Integer.parseInt(greenText.getText());
         blue = Integer.parseInt(blueText.getText());
         if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0)
        {
          JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!");
          redText.setText("0");
          blueText.setText("0");
          greenText.setText("0");
        }
         else
         {
                 //creates a new color using the values inputed by the user
        	 Color colorTest = new Color(red,green,blue);
          color = "" + red + green + blue;
          JOptionPane.showMessageDialog(null, color);
          //sets the color to the one created from user inputted values
          centerPanel.setBackground(colorTest);
         
         }
         
        }

ok thank you very much!

commented: Thank you for marking your thread as solved +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.