Question

In: Computer Science

Write an application with three buttons labeled “Red”, “Green”, and “Blue” that changes the background color...

Write an application with three buttons labeled “Red”, “Green”, and “Blue” that changes the background color of a panel in the center of the frame to red, green, or blue.

Add icons to the buttons of Exercise E19.1. Use a JButton constructor with an Icon argument and supply an ImageIcon.

this is the code that I already wrote that I need to do the above to.


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

public class backgroundColor extends JFrame
{

   private static final int FRAME_WIDTH = 500;
   private static final int FRAME_HEIGHT = 500;
   private JPanel colorPanel;
  
   public backgroundColor()
   {
       setTitle("Colored Buttons");
       createColorBackground();
       setSize(FRAME_WIDTH, FRAME_HEIGHT);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
   }
  
   private void createColorBackground()
   {
       colorPanel = new JPanel();
       add(colorPanel, BorderLayout.CENTER);
       buttonColors();
   }
  
   private void buttonColors()
   {
       JPanel buttonsPanel = createButtons();
       add(buttonsPanel, BorderLayout.SOUTH);
   }
  
   private JPanel createButtons()
   {
       JPanel buttonsPanel = new JPanel(new GridLayout(3, 1));
       buttonsPanel.add(createButton("Red", Color.RED));
       buttonsPanel.add(createButton("Green", Color.GREEN));
       buttonsPanel.add(createButton("Blue", Color.BLUE));
       return buttonsPanel;
   }
  
   private JButton createButton(String label, final Color color)
   {
       JButton button = new JButton(label);
       ActionListener listener = new backgroundColorListener();
       button.addActionListener(listener);
       return button;
   }
  
   class backgroundColorListener
   implements ActionListener
   {
       public void actionPerformed(ActionEvent action)
       {
           if(action.getActionCommand().contentEquals("Red"))
               colorPanel.setBackground(Color.red);
           else
               if(action.getActionCommand().contentEquals("Green"))
                   colorPanel.setBackground(Color.green);
               else
                   if(action.getActionCommand().contentEquals("Blue"))
                       colorPanel.setBackground(Color.blue);
       }
   }
   public static void main(String[] args)
   {
       new backgroundColor();
   }
}

Solutions

Expert Solution

PROGRAM :

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;


public class WindowApplication extends JFrame implements ActionListener{
  
JPanel colorPanel;
JPanel checkBoxPanel;
  
JCheckBox red;
JCheckBox green;
JCheckBox blue;
  
public WindowApplication() {
super("Window Application");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 378);
getContentPane().setLayout(null);
  
colorPanel = new JPanel();
colorPanel.setBounds(45, 21, 309, 186);
colorPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
getContentPane().add(colorPanel);
  
checkBoxPanel = new JPanel();
checkBoxPanel.setBounds(10, 262, 364, 54);
getContentPane().add(checkBoxPanel);
  
red = new JCheckBox("Red");
green = new JCheckBox("Green");
blue = new JCheckBox("Blue");
  
checkBoxPanel.add(red);
checkBoxPanel.add(green);
checkBoxPanel.add(blue);
  
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
  
this.setVisible(true);
  
}

@Override
public void actionPerformed(ActionEvent e) {
  
if(e.getSource().equals(red) || e.getSource().equals(green) || e.getSource().equals(blue)){
  
int redComponent = 0;
int greenComponent = 0;
int blueComponent = 0;
if(red.isSelected())
redComponent = 255;
if(green.isSelected())
greenComponent = 255;
if(blue.isSelected())
blueComponent = 255;
  
colorPanel.setBackground(new Color(redComponent, greenComponent, blueComponent));
this.repaint();
  
}
  
}
  
public static void main(String[] args) {

new WindowApplication();
  
}

}

OUTPUT ;


Related Solutions

Using the red, blue, yellow and green beads, devise and lay out a three color code...
Using the red, blue, yellow and green beads, devise and lay out a three color code for each of the following letters (codon). For example Z = green : red : green. In the spaces below the letter, record your “code”.                 E:            H:            I:            K:            L: _ _ __ _ __ _ __ _ __ _ __ _ _                O:           S:            T:            U: _ _ __ _ __ _ __ _ __ _ _ Create codons for:    Start:           ...
Write a MplabX IDE PIC10f200 code that work with three LEDs red, blue and green as...
Write a MplabX IDE PIC10f200 code that work with three LEDs red, blue and green as a mood light.( mood light has 7 colors. When all the leds terns on we will get white color, green and red tern on to get yellow, red to get red, red and blue tern on to get purple, just blue to get blue, blue and green to get light blue and just green led terns on to get green) then replay again.
a) Copper solutions are typically blue to blue green in color. What color of light would...
a) Copper solutions are typically blue to blue green in color. What color of light would they be expected to absorb in the greatest amount? Red Violet Blue Green b) Based on your answer to the previous question (hopefully you have the correct answer), what wavelength range would you expect to see the maximum absorption of light (in the visible range)? 500 nm to 600 nm 400 nm to 500 nm 300 nm to 400 nm 600 nm to 700...
14. There are eight balls in an urn, three red labeled {1, 2, 3}, a blue...
14. There are eight balls in an urn, three red labeled {1, 2, 3}, a blue ball marked {4}, and four white balls marked {5, 6, 7, 8}. Given the following three events, tell whether each possible pair of events (A and B, A and C, and B and C) is independent, disjoint, or neither. A. A red ball is drawn. B. An even numbered ball is drawn. C. A white ball is drawn.
There are three types of balls in a box: 5 red, 3 blue and 2 green....
There are three types of balls in a box: 5 red, 3 blue and 2 green. You draw 3 balls at once (without replacement) from this box and record: Y1=the # of red balls, Y2=the # of blue balls that you drew. Find the joint probability distribution of Y1, Y2, by first writing the possible values for y1, y2 in rows and columns and then filling in the probabilities within this table. Then check that the sum of the entries...
Suppose you have 24 items (three red, three blue, three yellow, and three green) to give...
Suppose you have 24 items (three red, three blue, three yellow, and three green) to give to 24 students. How many different ways can you distribute the things to the students? (Note: the items of the same color are identical)
A study of the effect of exposure to color (red or blue) on the ability to...
A study of the effect of exposure to color (red or blue) on the ability to solve puzzles used 42 subjects. Half the subjects (21) were asked to solve a series of puzzles while in a red-colored environment. The other half were asked to solve the same series of puzzles while in a bluecolored environment. The time taken to solve the puzzles was recorded for each subject. Due to the association of urgency with red, researchers believe those exposed to...
A study of the effect of exposure to color ( red or blue) on the ability...
A study of the effect of exposure to color ( red or blue) on the ability to solve puzzles used 48 subjects. Half the subjects were asked to solve a series of puzzles while in a red-colored environment. The other half were asked to solve the same series of puzzles while in a blue-colored environment. The 24 subjects in the red-colored environment had a mean time for solving the puzzles of 9.65 seconds with standard deviation 3.47 ; the 24...
Three fair dice colored red, blue and green are rolled. 1. What will you choose the...
Three fair dice colored red, blue and green are rolled. 1. What will you choose the sample space to be in this case? How many atomic events are there in the sample space? What probability distribution will you use to model this problem? 2. What is the probability that exactly two of the dice roll the same number? 3. What is the probability that all three dice roll distinct numbers? 4. What is the probability that at least two of...
A bag contains one red marble, two blue marbles and three green marbles. A marble is...
A bag contains one red marble, two blue marbles and three green marbles. A marble is selected at random. Define a random variable X such that X=1 if a red marble is selected, X=2 if a blue marble is selected and X=3 if a green marble is selected. Find the following probabilities. Hint: The blue marbles are different; you can label one blue-1 and the other blue-2. Similarly, each of the green marbles is different. 4a) Find P(X=1) 4b) Find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT