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;
}