AWT Color 颜色类

简介

Color 类声明默认 sRGB 颜色空间中的颜色或由 ColorSpace 标识的任意颜色空间中的颜色。

类声明

以下是 java.awt.Color 类的声明:

public class Color
    extends Object
    	implements Paint, Serializable

字段

以下是 java.awt.geom.Arc2D 类的字段:

  • static Color black -- The color black.

  • static Color BLACK -- The color black.

  • static Color blue -- The color blue.

  • static Color BLUE -- The color blue.

  • static Color cyan -- The color cyan.

  • static Color CYAN -- The color cyan.

  • static Color DARK_GRAY -- The color dark gray.

  • static Color darkGray -- The color dark gray.

  • static Color gray -- The color gray.

  • static Color GRAY -- The color gray.

  • static Color green -- The color green.

  • static Color GREEN -- The color green.

  • static Color LIGHT_GRAY -- The color light gray.

  • static Color lightGray -- The color light gray.

  • static Color magenta -- The color magenta.

  • static Color MAGENTA -- The color magenta.

  • static Color orange -- The color orange.

  • static Color ORANGE -- The color orange.

  • static Color pink -- The color pink.

  • static Color PINK -- The color pink.

  • static Color red -- The color red.

  • static Color RED -- The color red.

  • static Color white -- The color white.

  • static Color WHITE -- The color white.

  • static Color yellow -- The color yellow.

  • static Color YELLOW -- The color yellow.

类构造函数

S.N.构造函数 &描述
1

Color(ColorSpace cspace, float[] components, float alpha)

使用浮点数组中指定的颜色分量和指定的 alpha 在指定的 ColorSpace 中创建颜色。

2

Color(float r, float g, float b)

使用范围 (0.0 - 1.0) 内指定的红色、绿色和蓝色值创建不透明的 sRGB 颜色。

3

Color(float r, float g, float b, float a)

使用范围 (0.0 - 1.0)。

4

Color(int rgb)

使用指定的组合 RGB 值创建一个不透明的 sRGB 颜色,该值由位 16-23 中的红色成分、位 8-15 中的绿色成分和位 0-7 中的蓝色成分组成。

5

Color(int rgba, boolean hasalpha)

使用指定的组合 RGBA 值创建一个 sRGB 颜色,该值由位 24-31 中的 alpha 成分、位 16-23 中的红色成分、位 8-15 中的绿色成分和位 0-7 中的蓝色成分组成0-7。

6

Color(int r, int g, int b)

使用指定的红色、绿色和蓝色值在范围 (0 - 255) 内创建不透明的 sRGB 颜色。

7

Color(int r, int g, int b, int a)

使用指定的红色、绿色、蓝色和 alpha 值在范围 (0 - 255) 内创建 sRGB 颜色。

类方法

默认的 sRGB 空间。

S.N.方法与描述
1

Color brighter()

创建一个比此 Color 更亮的新 Color。

2

PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)

创建并返回用于生成纯色图案的 PaintContext。

3

Color darker()

创建一个比此 Color 更暗的新 Color。

4

static Color decrypt(String nm)

将字符串转换为整数并返回指定的不透明颜色。

5

boolean equals(Object obj)

确定另一个对象是否等于此颜色。

6

int getAlpha()

返回 0-255 范围内的 alpha 分量。

7

int getBlue()

返回默认 sRGB 空间中 0-255 范围内的蓝色分量。

8

static Color getColor(String nm)

在系统属性中查找颜色。

9

static Color getColor(String nm, Color v)

在系统属性中查找颜色。

10

static Color getColor(String nm, int v)

在系统属性中查找颜色。

11

float[] getColorComponents(ColorSpace cspace, float[] compArray)

返回一个浮点数组,该数组仅包含由 cspace 参数指定的 ColorSpace 中 Color 的颜色分量。

12

float[] getColorComponents(float[] compArray)

返回一个浮点数组,该数组仅包含 Color 的颜色分量,位于 Color 的 ColorSpace 中。

13

ColorSpace getColorSpace()

返回此 Color 的 ColorSpace。

14

float[] getComponents(ColorSpace cspace, float[] compArray)

返回一个浮点数组,该数组包含 Color 的颜色和 alpha 分量,位于 cspace 参数指定的 ColorSpace 中。

15

float[] getComponents(float[] compArray)

返回一个浮点数组,其中包含 Color 的颜色和 alpha 分量,位于 Color 的 ColorSpace 中。

16

int getGreen()

返回默认 sRGB 空间中 0-255 范围内的绿色分量。

17

static Color getHSBColor(float h, float s, float b)

根据 HSB 颜色模型的指定值创建 Color 对象。

18

int getRed()

返回默认 sRGB 空间中 0-255 范围内的红色分量。

19

int getRGB()

返回表示默认 sRGB ColorModel 中颜色的 RGB 值。

20

float[] getRGBColorComponents(float[] compArray)

返回一个浮点数组,该数组仅包含默认 sRGB 颜色空间中 Color 的颜色分量。

21

float[] getRGBComponents(float[] compArray)

返回一个浮点数组,该数组包含默认 sRGB 颜色空间中 Color 的颜色和 alpha 分量。

22

int getTransparency()

返回此颜色的透明度模式。

23

int hashCode()

计算此颜色的哈希码。

24

static int HSBtoRGB(float hue, float saturation, float bright)

将 HSB 模型指定的颜色成分转换为默认 RGB 模型的一组等效值。

25

static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals)

将颜色的成分(由默认 RGB 模型指定)转换为色调、饱和度和亮度的等效值集,这三个值是 HSB 模型的三个成分。

26

String toString()

返回此颜色的字符串表示形式。

继承的方法

此类从以下类继承方法:

  • java.lang.Object

Color 示例

使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui > 中创建以下 Java 程序

AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;        
      Font plainFont = new Font("Serif", Font.PLAIN, 24);        
      g2.setFont(plainFont);
      g2.setColor(Color.red);
      g2.drawString("Welcome to TutorialsPoint", 50, 70); 
      g2.setColor(Color.GRAY);
      g2.drawString("Welcome to TutorialsPoint", 50, 120);  
   }
}

使用命令提示符编译程序。转到 D:/ > AWT 并键入以下命令。

D:\AWT>javac com	utorialspoint\gui\AWTGraphicsDemo.java

如果没有错误,则表示编译成功。使用以下命令运行程序。

D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo

验证以下输出

AWT Color

awt_graphics.html