Using user input to change background color of JPanel

Thread Solved

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Using user input to change background color of JPanel

 
0
  #1
Oct 29th, 2008
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:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. /**
  7.  * Write a description of class ColorTestApplet here.
  8.  *
  9.  * @author (your name)
  10.  * @version (a version number or a date)
  11.  */
  12. public class ColorTestApplet extends JApplet
  13. {
  14. public void init()
  15. {
  16. this.setLayout(new BorderLayout());
  17.  
  18. //Declaring panels
  19. centerPanel = new JPanel();
  20. northPanel = new JPanel();
  21. southPanel = new JPanel();
  22.  
  23. //centerPanel.setLayout(new GridLayout());
  24. this.add(centerPanel, BorderLayout.CENTER);
  25. this.add(northPanel, BorderLayout.NORTH);
  26. this.add(southPanel, BorderLayout.SOUTH);
  27.  
  28. JLabel red = new JLabel("Red:");
  29. JLabel green = new JLabel("Green:");
  30. JLabel blue = new JLabel("Blue:");
  31.  
  32. redText = new JTextField("0", 5);
  33. greenText = new JTextField("0", 5);
  34. blueText = new JTextField("0", 5);
  35. redText.setHorizontalAlignment(redText.RIGHT);
  36. greenText.setHorizontalAlignment(redText.RIGHT);
  37. blueText.setHorizontalAlignment(redText.RIGHT);
  38.  
  39. JButton show = new JButton("Show");
  40.  
  41. JLabel name = new JLabel("Kimberlie Davis");
  42.  
  43. northPanel.add(red);
  44. northPanel.add(redText);
  45. northPanel.add(green);
  46. northPanel.add(greenText);
  47. northPanel.add(blue);
  48. northPanel.add(blueText);
  49. southPanel.add(show);
  50. centerPanel.add(name);
  51.  
  52. ShowAction showAction = new ShowAction();
  53. show.addActionListener(showAction);
  54. }
  55.  
  56. class ShowAction implements ActionListener
  57. {
  58. public void actionPerformed(ActionEvent e)
  59. {
  60. int red, green, blue;
  61. String color;
  62. try
  63. {
  64. red = Integer.parseInt(redText.getText());
  65. green = Integer.parseInt(greenText.getText());
  66. blue = Integer.parseInt(blueText.getText());
  67. if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0)
  68. {
  69. JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!");
  70. redText.setText("0");
  71. blueText.setText("0");
  72. greenText.setText("0");
  73. }
  74. else
  75. {
  76. color = "" + red + green + blue;
  77. JOptionPane.showMessageDialog(null, color);
  78. //centerPanel.setBackground(red, blue, green);
  79. }
  80. //centerpanel.setBackground(Color.color);
  81. }
  82. catch(NumberFormatException exception)
  83. {
  84. JOptionPane.showMessageDialog(null, "Please enter a number!");
  85. redText.setText("0");
  86. blueText.setText("0");
  87. greenText.setText("0");
  88. }
  89. }
  90. }
  91.  
  92. JPanel centerPanel;
  93. JPanel northPanel;
  94. JPanel southPanel;
  95.  
  96. JTextField redText;
  97. JTextField greenText;
  98. JTextField blueText;
  99. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Using user input to change background color of JPanel

 
0
  #2
Oct 29th, 2008
Do you have to do it that way? I think a much more interesting way would be to use ColorChooser
Last edited by jasimp; Oct 29th, 2008 at 9:21 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Using user input to change background color of JPanel

 
0
  #3
Oct 29th, 2008
I do have to have the user input numbers for red green and blue. Its for a school assignment.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Using user input to change background color of JPanel

 
1
  #4
Oct 29th, 2008
Just create a new color using your values. I added the line of code into your try statement.

  1. try
  2. {
  3. red = Integer.parseInt(redText.getText());
  4. green = Integer.parseInt(greenText.getText());
  5. blue = Integer.parseInt(blueText.getText());
  6. if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0)
  7. {
  8. JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!");
  9. redText.setText("0");
  10. blueText.setText("0");
  11. greenText.setText("0");
  12. }
  13. else
  14. {
  15. //creates a new color using the values inputed by the user
  16. Color colorTest = new Color(red,green,blue);
  17. color = "" + red + green + blue;
  18. JOptionPane.showMessageDialog(null, color);
  19. //sets the color to the one created from user inputted values
  20. centerPanel.setBackground(colorTest);
  21.  
  22. }
  23.  
  24. }
Last edited by jasimp; Oct 29th, 2008 at 9:24 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Using user input to change background color of JPanel

 
1
  #5
Oct 29th, 2008
ok thank you very much!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC