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

Dani AI

Generated

is on the right track. Two tweaks will make this rock-solid for an assignment. First, since you are using a JApplet, build the UI on the Event Dispatch Thread. In init(), wrap your UI creation in SwingUtilities.invokeAndWait so the browser does not return from init before the GUI is ready. Example:

public void init() {
  try {
    SwingUtilities.invokeAndWait(this::buildUI);
  } catch (Exception ex) {
    ex.printStackTrace();
  }
}
private void buildUI() {
  // move your existing component setup here
}

This follows Swing’s threading rules for applets. (docs.oracle.com)

To avoid parsing errors and manual range checks, consider swapping the three JTextFields for spinners that already constrain input to 0..255:

JSpinner r = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
JSpinner g = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
JSpinner b = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1));
// add r,g,b to your northPanel; in the button handler read (Integer) r.getValue(), etc.

This keeps the UI responsive and the values valid by construction; your existing button listener can then use those ints to update the center panel’s color. (docs.oracle.com)

If you must keep text fields, use a formatter so only integers in range are accepted:

NumberFormatter nf = new NumberFormatter(NumberFormat.getIntegerInstance());
nf.setMinimum(0); nf.setMaximum(255); nf.setAllowsInvalid(false);
JFormattedTextField redText = new JFormattedTextField(nf);

This removes the need for most of the try/catch logic. Also note that new java.awt.Color(r,g,b) will itself throw IllegalArgumentException if any component is outside 0..255, which you can leverage as a final safety net. (docs.oracle.com)

Side note for future work: applets are obsolete today; the same panel logic dropped into a JFrame will be easier to run locally.

Recommended Answers

All 4 Replies

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.