Java AWT |颜色类

Color类是Java抽象窗口工具包(AWT)包的一部分。Color类使用给定的RGBA值创建颜色,其中 RGBA 代表红色、绿色、蓝色、ALPHA或蓝色 HSB HSB代表色调、饱和度和颜色成分的值。单个组件RGBA的值范围为0到255或0.0到0.1。alpha的值决定颜色的不透明度,其中0或0.0表示完全透明,255或1.0表示不透明。

null

颜色类的构造函数

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

颜色类中常用的方法

方法 解释
更明亮的 创建一种新颜色,它是该颜色的更亮版本。
createContext(颜色模型cm、矩形r、矩形2D r2d、仿射变换x、渲染提示h) 创建并返回用于生成纯色场图案的PaintContext。
深色的 /td> 创建一种新颜色,该颜色为该颜色的较暗版本。
解码(字符串nm) 将字符串转换为整数并返回指定的不透明颜色。
等于(对象obj) 确定另一个颜色对象是否与此颜色相同。
getAlpha() 返回0-255范围内的alpha分量。
getBlue() 返回0-255范围内的蓝色分量。
getColor(字符串nm) 在系统属性中查找颜色。
getColor(字符串nm,颜色v) 在系统属性中查找颜色。
getColor(字符串nm,int v) 在系统属性中查找颜色。
getColorComponents(颜色空间cspace,float[]compArray) 返回一个浮点数组,该数组只包含cspace参数指定的颜色空间中颜色的颜色分量。
getColorComponents(float[]compArray) 在颜色的颜色空间中,返回仅包含颜色的颜色分量的浮点数组。
getColorSpace() 返回此颜色的颜色空间。
getGreen() 返回默认sRGB空间中0-255范围内的绿色组件。
getRed() 返回默认sRGB空间中0-255范围内的红色分量。
getRGB() 返回表示默认sRGB颜色模型中颜色的RGB值。
getHSBColor(浮点h、浮点s、浮点b) 基于HSB颜色模型的指定值创建颜色对象。
getTransparency() 返回此颜色的透明度模式。
hashCode() 计算此颜色的哈希代码。
HSBtoRGB(浮子h、浮子s、浮子b) 将HSB值转换为RGB值
RGBtoHSB(整数r、整数g、整数b、浮点[]hsbvals) 将RGB值转换为HSB值

以下程序演示了Java AWT中的颜色类:

  • 程序使用类的常量中指定的颜色设置面板的背景色

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


输出:

图片[1]-Java AWT |颜色类-yiteyi-C++库

  • 通过说明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();
}
}


输出:

图片[2]-Java AWT |颜色类-yiteyi-C++库

  • 通过说明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();
}
}


输出:

图片[3]-Java AWT |颜色类-yiteyi-C++库

  • 程序使用颜色(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();
}
}


输出:

图片[4]-Java AWT |颜色类-yiteyi-C++库

  • 程序从用户处获取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();
}
}


输出:

图片[5]-Java AWT |颜色类-yiteyi-C++库

  • 从用户处获取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();
}
}


输出:

图片[6]-Java AWT |颜色类-yiteyi-C++库

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

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享