How to Use Color Class in Java - With Examples

manish174's Avatar author of How to Use Color Class in Java - With Examples
This is an article on How to Use Color Class in Java - With Examples in Java.
Here we go. This example is used to illustrate that “how we can use color class in java?” We several time need to color our panel, label, or form. For this java provide us Color class in awt package i.e “”.

This class has seven - number of constructor. But the widely used constructors are following:
  1. Color(int r, int g, int b) : In this constructor we have to pass 3 argument of integer type ranging from 0-255, i.e. for Red, Green and Blue or usually know as RGB value.
  2. Color(int r, int g, int b, int a) : This constructor creates a RGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
  3. Color(int rgb) : This constructor is used to pass the RGB value by a single integer value, i.e. all the three color code will have the same value at a particular time.

Now following are the examples listing the use of Color class in java.

Example 1: Simple program to color a form or label or panel. In this program I have three panels p1 ,p2 and p3.

P1 panel has been colored with constructor having three different integer values, i.e. with RGB value 23, 32, 13 respectively.

P2 panel has been colored with constructor having a single integer value passed as an argument, i.e. with RGB values 123, 123, 123 respectively.

P3 panel is added simply to the form, i.e. with no background.

Code: JAVA
import java.awt.*;
import javax.swing.*;
class ColorForm
    JFrame f1;
    JPanel p1, p2, p3;
    GridLayout gl;

        f1 = new JFrame( "Color RGB Use" );
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();

        gl = new GridLayout(3,1);

        f1.getContentPane().add( p1 );
        f1.getContentPane().add( p2 );
        f1.getContentPane().add( p3 );

        f1.setSize( 400, 300 );
        f1.setVisible( true );
        f1.setLayout( gl );

        Color pan1 = new Color( 23, 32, 13 );
        p1.setBackground( pan1 );

        Color pan2 = new Color( 123 );
        p2.setBackground( pan2 );

        f1.setDefaultCloseOperation( f1.EXIT_ON_CLOSE );

    public static void main( String asd[] )
        new ColorForm();

Example 2: This example is advance of above. In this the color has been changed using text boxes and applied via a click on button named “Fill Color”. For this purpose we have to use an action listener.

Here three text boxes have been declared to take the value of RGB as an input. Then on click button an action listener is activated so that every time we click, the start its work and change the color depending upon this RGB value.

Code: JAVA
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class fill_c implements ActionListener
    JFrame f;
    JPanel p;
    JButton b;
    JTextField tf1, tf2, tf3;

        f= new JFrame( "Color" );
        b= new JButton( "Fill Color" );
        tf1= new JTextField( 5 );
        tf2= new JTextField( 5 );
        tf3= new JTextField( 5 );

        p= new JPanel();

        f.setVisible( true );
        f.setSize( 255, 255 );

        b.addActionListener( this );

        f.add( p );
        p.add( tf1 );
        p.add( tf2 );
        p.add( tf3 );
        p.add( b );

        f.setDefaultCloseOperation( f.EXIT_ON_CLOSE );

    public void actionPerformed( ActionEvent ae )

        int r, g , b;
        String rr, gg, bb;

        rr= tf1.getText();
        gg= tf2.getText();
        bb= tf3.getText();

        r = Integer.parseInt( rr );
        g = Integer.parseInt( gg );
        b = Integer.parseInt( bb );

        Color x= new Color( r, g, b );
        p.setBackground( x );


    public static void main( String s[] )
        new fill_c();
shabbir like this
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
If you liked this article do nominate this article for Article of the month - Sep 2010 Before 15th of October 2010.
fashionbop's Avatar
Go4Expert Member
I would like to appreciate this kind of article to review my java's skill every day!
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Article selected for voting for Article of the month - Sep 2010. Cast your vote now before October 27th, 2010
isaacogbos's Avatar, Join Date: Apr 2012
Newbie Member
pls i want to do a project on cloud computing with erp(enterprise resource plannin) pls i need java source code to cover this cuz my project is incomplete without the codes...
suresh091221's Avatar, Join Date: Jul 2012
Newbie Member
i want to do project in java or .net plz help to chose the project topic or the coding of this project