How to Use Color Class in Java - With Examples

Discussion in 'Java' started by manish174, Sep 19, 2010.

  1. manish174

    manish174 New Member

    Joined:
    Sep 17, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    New Delhi
    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 “java.awt.Color.java”.

    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:
    import java.awt.*;
    import javax.swing.*;
    class ColorForm
    {
    	JFrame f1;
    	JPanel p1, p2, p3;
    	GridLayout gl;
    
    	ColorForm()
    	{
    		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:
    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;
    
    
    	fill_c()
    	{
    		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 likes this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    http://www.fashion-bop.com
    I would like to appreciate this kind of article to review my java's skill every day!
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. isaacogbos

    isaacogbos New Member

    Joined:
    Apr 17, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Question on java

    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...
     
  6. suresh091221

    suresh091221 New Member

    Joined:
    Jul 23, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i want to do project in java or .net plz help to chose the project topic or the coding of this project
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice