2/21/12

RGB Color Chooser Applet Java Code

A RGB Color Chooser shows three sliders that the user can manipulate to set the red, green, and blue, components of a color. A color box shows the selected color, and there are three labels that show the numerical values of all the components. Values are in the range 0 to 255. The initial color is black.


An Applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. 

The program implements an interface called ChangeListener. It defines an object which listens for ChangeEvents. The function stateChange() is a method of the ChangeListener which is invoked when the target of the listener is changed.


Program Code :



/***************************************************************
*     Code 2 Learn Sample Code - http://www.code2learn.com     *
*                                                              *
*                                                              *
*    Please direct all inquiries at codeme@code2learn.com      *
***************************************************************/

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

public class RGMColorChooser extends JApplet
                       implements ChangeListener {

   private JSlider redSlider, greenSlider, blueSlider;

   private JLabel redLabel, greenLabel, blueLabel;

   private JPanel colorbox;
              // Color box for displaying the color.

   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 values. */

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

       /* Set the colors of the labels */

       redLabel.setBackground(Color.white);
       redLabel.setForeground(Color.red);
       redLabel.setOpaque(true);
       greenLabel.setBackground(Color.white);
       greenLabel.setForeground(new Color(0,150,0));
       greenLabel.setOpaque(true);
       blueLabel.setBackground(Color.white);
       blueLabel.setForeground(Color.blue);
       blueLabel.setOpaque(true);

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

       redSlider.addChangeListener(this);
       greenSlider.addChangeListener(this);
       blueSlider.addChangeListener(this);

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

       colorbox = new JPanel();
       colorbox.setBackground(Color.black);

       /* Create the applet layout, which consists of a row of
          three equal-sized regions holding the JSliders,
          the Labels, and the color box. */

       setBackground(Color.gray);
       getContentPane().setBackground(Color.gray);

       getContentPane().setLayout(new GridLayout(1,3,3,3));
       JPanel scrolls = new JPanel();
       JPanel labels = new JPanel();
       scrolls.setBackground(Color.gray);
       labels.setBackground(Color.gray);
       getContentPane().add(scrolls);
       getContentPane().add(labels);
       getContentPane().add(colorbox);

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

       scrolls.setLayout(new GridLayout(3,1,2,2));
       scrolls.add(redSlider);
       scrolls.add(greenSlider);
       scrolls.add(blueSlider);

       labels.setLayout(new GridLayout(3,1,2,2));
       labels.add(redLabel);
       labels.add(greenLabel);
       labels.add(blueLabel);

   } 


@Override
    public void stateChanged(ChangeEvent evt) {
        // TODO Auto-generated method stub
        /* This is called when the 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 box 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);
        colorbox.setBackground(new Color(r,g,b));

    
}

}  

SHARE THIS POST:

Related Posts:

  • Symbol Table in Java Symbol Table Definition : In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with inform… Read More
  • HashMap simple usage and understanding Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated. The HashMap class uses a hash table to implementation of the map interface. This allows… Read More
  • Java : Encryption and Decryption of Data using AES algorithm with example code There are many problems when you try encrypting a string such password, credit card nos, phone no. etc ie 1. which algorithm to use. 2. how to store the generated Key in the database. 3. should i use MD5, AES etc. Here is … Read More
  • Reading and Writing File in Java Java like other programming languages supports both reading and writing of files. For reading purpose we use FileReader and for writing we use FileWriter. FileReader Class : The FileReader class creates a Reader that … Read More
  • Creating a Basic JDBC application Today we will be creating a Basic JDBC application i.e. an application that will be able to retrieve information (student_name, roll_no, class, DOB, etc) from the database. I already gave a tutorial on connecting SQL Server… Read More

2 comments:

  1. Hello.... informative read.. being enrolled in a JAVA programming course @ http://www.wiziq.com/course/1617-core-java-for-beginners-icse-students I was looking for such material on the net to boost up my preparation.. thanks :)

    ReplyDelete
  2. @Srishti Dubey Thank You.. For any more information don't hesitate to write to us on codeme@code2learn.com

    ReplyDelete