Java AWT | Color Class

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

  1. 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.
  2. Color(float r, float g, float b) : creates a opaque color with specified RGB components(values are in range 0.0 – 0.1)
  3. Color(float r, float g, float b, float a) : creates a color with specified RGBA components(values are in range 0.0 – 0.1)
  4. 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.
  5. 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.
  6. Color(int r, int g, int b) : Creates a opaque color with specified RGB components(values are in range 0 – 255)
  7. 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

method explanantion
brighter() creates a new Color that is a brighter version of this Color.
createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform x, RenderingHints h) creates and returns a PaintContext used to generate a solid color field pattern.
darker() /td>

creates a new Color that is a darker version of this Color.
decode(String nm) converts a String to an integer and returns the specified opaque Color.
equals(Object obj) determines whether another Color object is equal to this Color.
getAlpha() returns the alpha component in the range 0-255.
getBlue() returns the blue component in the range 0-255 .
getColor(String nm) Finds a color in the system properties.
getColor(String nm, Color v) Finds a color in the system properties.
getColor(String nm, int v) Finds a color in the system properties.
getColorComponents(ColorSpace cspace, float[] compArray) returns a float array containing only the color components of the Color in the ColorSpace specified by the cspace parameter.
getColorComponents(float[] compArray) returns a float array containing only the color components of the Color, in the ColorSpace of the Color.
getColorSpace() returns the ColorSpace of this Color.
getGreen() returns the green component in the range 0-255 in the default sRGB space.
getRed() returns the red component in the range 0-255 in the default sRGB space.
getRGB() Returns the RGB value representing the color in the default sRGB ColorModel.
getHSBColor(float h, float s, float b) Creates a Color object based on the specified values for the HSB color model.
getTransparency() returns the transparency mode for this Color.
hashCode() computes the hash code for this Color.
HSBtoRGB(float h, float s, float b) Converts the HSB value to RGB value
RGBtoHSB(int r, int g, int b, float[] hsbvals) converts the RGB value to HSB value

Below programs illustrate the Color class in Java AWT :

  1. Program to set the background color of panel using the color specified in the constants of the class
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 colour extends JFrame {
      
        // constructor
        colour()
        {
            super("colour");
      
            // 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[])
        {
            colour c = new colour();
        }
    }

    chevron_right

    
    

    Output :

  2. Program to create a new Color by stating the RGB value and set it as background of panel
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a new Color by stating
    // the RGB value and set it as background of panel 
    import java.awt.*;
    import javax.swing.*;
      
    class colour extends JFrame {
      
        // constructor
        colour()
        {
            super("colour");
      
            // 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[])
        {
            colour c = new colour();
        }
    }

    chevron_right

    
    

    Output :

  3. Program to create a new Color by stating the RGB value and alpha value, set it as background of panel
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 colour extends JFrame {
      
        // constructor
        colour()
        {
            super("colour");
      
            // 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[])
        {
            colour c = new colour();
        }
    }

    chevron_right

    
    

    Output :

  4. Program to create a new Color by using Color(int rgb) method, set it as background of panel
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 colour extends JFrame {
      
        // constructor
        colour()
        {
            super("colour");
      
            // 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[])
        {
            colour c = new colour();
        }
    }

    chevron_right

    
    

    Output :

  5. Program to take RGBA value from user and set it as background of panel
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to take RGBA value from 
    // user and set it as background of panel
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
      
    class colour extends JFrame implements ActionListener {
      
        // textfield to enter RGBA value
        JTextField R, G, B, A;
      
        // panel
        JPanel p;
      
        // constructor
        colour()
        {
            super("colour");
      
            // 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 velue
                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[])
        {
            colour c = new colour();
        }
    }

    chevron_right

    
    

    Output :

  6. Program to take HSB value from user and set it as background of panel
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to take HSB value from 
    // user and set it as background of panel
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
      
    class colour extends JFrame implements ActionListener {
        // textfield to enter RGBA value
        JTextField H, S, B;
      
        // panel
        JPanel p;
      
        // constructor
        colour()
        {
            super("colour");
      
            // 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 velue
                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[])
        {
            colour c = new colour();
        }
    }

    chevron_right

    
    

    Output :

Reference : https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.