This is an Applet that when run displays three sliders that can be slided to to set a color by mixing red, green and blue colors. A color patch on the applet shows the selected color. There are three labels that show the numerical values of all the colors. Values are in the range 0 to 255. By default, the initial color is black when applet is run. This program uses AWT components and implements ChangeListener interface for event handling. ChangeListener interface listens for ChangeEvents. The function stateChange() is a method of the ChangeListener which is invoked when the target of the listener is changed. In this example, the targets are the three sliders whose values change when user slides them to any direction.

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

public class RGBColorPicker extends JApplet
                       implements ChangeListener {

   private JSlider redSlider, greenSlider, blueSlider;
   private JLabel redLabel, greenLabel, blueLabel;
   private JPanel colorPatch;
   public void init() {

       // Create JSliders with possible values from 0 to 255. 

       redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
       greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
       blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);

       // Create JLabels showing current RGB and HSB values. 

       redLabel = new JLabel(" R = 0");
       greenLabel = new JLabel(" G = 0");
       blueLabel = new JLabel(" B = 0");

       // Set the colors of the labels, and make them opaque 

       greenLabel.setForeground(new Color(0,150,0));

       // Set the applet to listen for changes to the JSliders' values 


       // Create a JPanel whose background color will always be set to the currently selected color.  Otherwise, the panel is empty.

       colorPatch = new JPanel();

       // Create the applet layout, which consists of a row of
       // three equal-sized regions holding JSliders,
       // Labels, and the color patch. The background color
       // of the applet is gray, which will show around edges
       // and between components. 


       getContentPane().setLayout(new GridLayout(1,3,3,3));
       JPanel scrolls = new JPanel();
       JPanel labels = new JPanel();

       // Add the JSliders and the JLabels to their respective panels. 

       scrolls.setLayout(new GridLayout(3,1,2,2));

       labels.setLayout(new GridLayout(3,1,2,2));

   } // end init();

   public void stateChanged(ChangeEvent evt) {
       // This is called when user has changed the value on
       // one of the sliders. All the sliders are checked,
       // the labels are set to display the correct values,
       // and the color patch is set to correspond to the new color.
       int r = redSlider.getValue();
       int g = greenSlider.getValue();
       int b = blueSlider.getValue();
       redLabel.setText(" R = " + r);
       greenLabel.setText(" G = " + g);
       blueLabel.setText(" B = " + b);
       colorPatch.setBackground(new Color(r,g,b));
   } // end stateChanged

   public Insets getInsets() {
      // This method is used to insert padding between 
      // the edges of the applet and the components that
      // it contains.
      return new Insets(3,3,3,3);

}  // end class RGBColorPicker
Tagged with: AndroidJAVASource Code

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.