Java AWT | Color Class - GeeksforGeeks

01 Dec, 2021
The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha determines the opacity of the color, where 0 or 0.0 stands fully transparent and 255 or 1.0 stands opaque. 
Constructors of Color Class
 
Color(ColorSpace c, float[] co, float a) : Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.Color(float r, float g, float b) : creates a opaque color with specified RGB components(values are in range 0.0 – 0.1)Color(float r, float g, float b, float a) : creates a color with specified RGBA components(values are in range 0.0 – 0.1)Color(int rgb): Creates an opaque RGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8 – 15, and the blue component in bits 0-7.Color(int rgba, boolean b): Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16 – 23, the green component in bits 8 – 15, and the blue component in bits 0 – 7.Color(int r, int g, int b) : Creates a opaque color with specified RGB components(values are in range 0 – 255)Color(int r, int g, int b, int a) : Creates a color with specified RGBA components(values are in range 0 – 255)
Color(ColorSpace c, float[] co, float a) : Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.
Color(float r, float g, float b) : creates a opaque color with specified RGB components(values are in range 0.0 – 0.1)
Color(float r, float g, float b, float a) : creates a color with specified RGBA components(values are in range 0.0 – 0.1)
Color(int rgb): Creates an opaque RGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8 – 15, and the blue component in bits 0-7.
Color(int rgba, boolean b): Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16 – 23, the green component in bits 8 – 15, and the blue component in bits 0 – 7.
Color(int r, int g, int b) : Creates a opaque color with specified RGB components(values are in range 0 – 255)
Color(int r, int g, int b, int a) : Creates a color with specified RGBA components(values are in range 0 – 255)
 
Commonly Used Methods In Color Class
 
Below programs illustrate the Color class in Java AWT :
Program to set the background color of panel using the color specified in the constants of the class
Java
// Java program to set the background color of panel// using the color specified in the constants// of the class.import java.awt.*;import javax.swing.*; class color extends JFrame {     // constructor    color()    {        super("color");         // create a new Color        Color c = Color.yellow;         // create a panel        JPanel p = new JPanel();         // set the background of the frame        // to the specified Color        p.setBackground(c);         setSize(200, 200);        add(p);        show();    }     // Main Method    public static void main(String args[])    {        color c = new color();    }}
Output : 
Program to create a new Color by stating the RGB value and set it as background of panel
Java
// Java program to create a new Color by stating// the RGB value and set it as background of panelimport java.awt.*;import javax.swing.*; class color extends JFrame {     // constructor    color()    {        super("color");         // create a new Color        // RGB value of Yellow is 225, 255, 0        Color c = new Color(255, 255, 0);         // create a panel        JPanel p = new JPanel();         // set the background of the        // frame to the specified Color        p.setBackground(c);         setSize(200, 200);        add(p);        show();    }     // Main Method    public static void main(String args[])    {        color c = new color();    }}
Output :
Program to create a new Color by stating the RGB value and alpha value, set it as background of panel
Java
// Java program to create a new Color by stating the// RGB value and alpha value, set it as background// of panel .import java.awt.*;import javax.swing.*; class color extends JFrame {     // constructor    color()    {        super("color");         // create a new Color        // RGB value of red is 225, 0, 0        // and set its alpha value as        // 100 out of 255        Color c = new Color(255, 0, 0, 100);         // create a panel        JPanel p = new JPanel();         // set the background of the       // frame to the specified Color        p.setBackground(c);         setSize(200, 200);        add(p);        show();    }     // Main Method    public static void main(String args[])    {        color c = new color();    }}
Output :
Program to create a new Color by using Color(int rgb) method, set it as background of panel
Java
// Java program to create a new Color by using// Color(int rgb) method, set it as background// of panel .import java.awt.*;import javax.swing.*; class color extends JFrame {     // constructor    color()    {        super("color");         // create a new Color        // RGB value of blue is 255        // and set its alpha value as        // 200 out of 255        Color c = new Color(255);         // create a panel        JPanel p = new JPanel();         // set the background of the       // frame to the specified Color        p.setBackground(c);         setSize(200, 200);        add(p);        show();    }     // Main Method    public static void main(String args[])    {        color c = new color();    }}
Output :
Program to take RGBA value from user and set it as background of panel
Java
// Java Program to take RGBA value from// user and set it as background of panelimport java.awt.*;import javax.swing.*;import java.awt.event.*; class color extends JFrame implements ActionListener {     // textfield to enter RGBA value    JTextField R, G, B, A;     // panel    JPanel p;     // constructor    color()    {        super("color");         // create textfield        R = new JTextField(3);        G = new JTextField(3);        B = new JTextField(3);        A = new JTextField(3);         // create labels        JLabel l = new JLabel("Red= ");        JLabel l1 = new JLabel("Green= ");        JLabel l2 = new JLabel("Blue= ");        JLabel l3 = new JLabel("Alpha= ");         // create a panel        p = new JPanel();         // create button        JButton b = new JButton("ok");        JButton b1 = new JButton("brighter");        JButton b2 = new JButton("Darker");         // add ActionListener        b.addActionListener(this);        b2.addActionListener(this);        b1.addActionListener(this);         // add components to panel        p.add(l);        p.add(R);        p.add(l1);        p.add(G);        p.add(l2);        p.add(B);        p.add(l3);        p.add(A);        p.add(b);        p.add(b1);        p.add(b2);         setSize(200, 200);        add(p);        show();    }     // if button is pressed    public void actionPerformed(ActionEvent evt)    {        String s = evt.getActionCommand();        if (s.equals("ok")) {            int r, g, b, a;             // get rgba value            r = Integer.parseInt(R.getText());            g = Integer.parseInt(G.getText());            b = Integer.parseInt(B.getText());            a = Integer.parseInt(A.getText());             // create a new Color            Color c = new Color(r, g, b, a);             // set the color as background of panel            p.setBackground(c);        }        else if (s.equals("brighter")) {             // getBackgroundColor            Color c = p.getBackground();             // make the color brighter            c = c.brighter();             // set the color as background of panel            p.setBackground(c);        }        else {             // getBackgroundColor            Color c = p.getBackground();             // make the color brighter            c = c.darker();             // set the color as background of panel            p.setBackground(c);        }    }     // Main Method    public static void main(String args[])    {        color c = new color();    }}
Output :
Program to take HSB value from user and set it as background of panel
Java
// Java Program to take HSB value from// user and set it as background of panelimport java.awt.*;import javax.swing.*;import java.awt.event.*; class color extends JFrame implements ActionListener {    // textfield to enter RGBA value    JTextField H, S, B;     // panel    JPanel p;     // constructor    color()    {        super("color");         // create textfield        H = new JTextField(3);        S = new JTextField(3);        B = new JTextField(3);         // create labels        JLabel l = new JLabel("Hue= ");        JLabel l1 = new JLabel("Saturation= ");        JLabel l2 = new JLabel("Brightness= ");         // create a panel        p = new JPanel();         // create button        JButton b = new JButton("ok");        JButton b1 = new JButton("brighter");        JButton b2 = new JButton("Darker");         // add ActionListener        b.addActionListener(this);        b2.addActionListener(this);        b1.addActionListener(this);         // add components to panel        p.add(l);        p.add(H);        p.add(l1);        p.add(S);        p.add(l2);        p.add(B);        p.add(b);        p.add(b1);        p.add(b2);         setSize(200, 200);        add(p);        show();    }     // if button is pressed    public void actionPerformed(ActionEvent evt)    {        String st = evt.getActionCommand();        if (st.equals("ok")) {            float h, s, b;             // get rgba value            h = Float.parseFloat(H.getText());            s = Float.parseFloat(S.getText());            b = Float.parseFloat(B.getText());             // create a new Color            Color c = Color.getHSBColor(h, s, b);             // set the color as background of panel            p.setBackground(c);        }        else if (st.equals("brighter")) {             // getBackgroundColor            Color c = p.getBackground();             // make the color brighter            c = c.brighter();             // set the color as background of panel            p.setBackground(c);        }        else {             // getBackgroundColor            Color c = p.getBackground();             // make the color brighter            c = c.darker();             // set the color as background of panel            p.setBackground(c);        }    }     // Main Method    public static void main(String args[])    {        color c = new color();    }}
Output :
Reference : https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
ManasChhabra2
sweetyty
abhishek0719kadiyan
Java-AWT
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
ArrayList in Java
Initialize an ArrayList in Java
Singleton Class in Java
Overriding in Java
Collections in Java
Multithreading in Java
Set in Java
LinkedList in Java
Stream In Java