📜  Java AWT |颜色等级

📅  最后修改于: 2022-05-13 01:54:57.882000             🧑  作者: Mango

Java AWT |颜色等级

Color 类是Java Abstract Window Toolkit(AWT) 包的一部分。 Color 类通过使用给定的 RGBA 值(其中RGBA代表 RED、GREEN、BLUE、ALPHA)或使用HSB值(其中 HSB 代表 HUE、SATURATION、BRIcomponents)来创建颜色。单个分量 RGBA 的值范围为 0 到 255 或 0.0 到 0.1。 alpha 的值决定了颜色的不透明度,其中 0 或 0.0 表示完全透明,255 或 1.0 表示不透明。

颜色类的构造函数

  1. Color(ColorSpace c, float[] co, float a) :使用浮点数组中指定的颜色分量和指定的 alpha 在指定的 ColorSpace 中创建颜色。
  2. Color(float r, float g, float b) : 创建具有指定 RGB 分量的不透明颜色(值在 0.0 – 0.1 范围内)
  3. Color(float r, float g, float b, float a) : 创建具有指定 RGBA 分量的颜色(值在 0.0 – 0.1 范围内)
  4. Color(int rgb) :使用指定的组合 RGB 值创建不透明的 RGB 颜色,该值由 16-23 位中的红色分量、8-15 位中的绿色分量和 0-7 位中的蓝色分量组成。
  5. Color(int rgba, boolean b) :创建具有指定组合 RGBA 值的 sRGB 颜色,该值由第 24-31 位中的 alpha 分量、第 16-23 位中的红色分量、第 8 位中的绿色分量组成
    – 15,以及位 0 – 7 中的蓝色分量。
  6. Color(int r, int g, int b) : 创建具有指定 RGB 分量的不透明颜色(值在 0 – 255 范围内)
  7. Color(int r, int g, int b, int a) :创建具有指定 RGBA 分量的颜色(值在 0 – 255 范围内)

颜色类常用方法

methodexplanation
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

下面的程序说明了Java AWT 中的 Color 类:

  • 使用类常量中指定的颜色设置面板背景颜色的程序
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();
    }
}


Java
// 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 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();
    }
}


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();
    }
}


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();
    }
}


Java
// 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 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();
    }
}


Java
// 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 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();
    }
}


输出 :

  • 程序通过声明 RGB 值来创建新颜色并将其设置为面板的背景

Java

// 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 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();
    }
}

输出 :

  • 通过指定 RGB 值和 alpha 值创建新颜色的程序,将其设置为面板的背景

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();
    }
}

输出 :

  • 使用 Color(int rgb) 方法创建新颜色的程序,将其设置为面板的背景

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();
    }
}

输出 :

  • 程序从用户那里获取 RGBA 值并将其设置为面板的背景

Java

// 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 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();
    }
}

输出 :

  • 程序从用户那里获取 HSB 值并将其设置为面板的背景

Java

// 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 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();
    }
}

输出 :

参考: https: Java/awt/Color.html