java color类 蓝色,Java中的颜色类

I have a question regarding the awt Color class in Java.

I am currently using the class abbreviations such as Color.RED and Color.BLACK. I also have a list of three integers such as the following:

int var1 = 0

int var2 = 0

int var3 = 255

Is there a method to convert these integers into the appropriate Java Color name?

解决方案

There is no way to do this with a single method in the Java core classes. However, you can fairly easily do this yourself in two ways.

First way

Create a new Color out of the RGB values you have:

Color color = new Color(var1, var2, var3);

Then

Get the Class object from the Color class with getClass().

Get the elements from that with getEnumConstants().

Stream it using Arrays.stream()

Filter it by calling filter(), so it only contains all the enum constants that equal the color you made (there should be either one or zero).

Use toArray() to turn the stream into an array.

Get the first element of that array with [0]. This will throw an ArrayIndexOutOfBoundsException if there isn't a predefined color matching your color.

Get the name of that color with Enum's toString().

String colorName = Arrays.stream(Color.getClass().getEnumConstants())

.filter(c -> c.equals(color))

.toArray()[0]

.toString();

Second way

First, create a HashMap of Colors that contains all the colors you want:

HashMap colors = new HashMap();

colors.put(Color.BLACK, "BLACK");

colors.put(Color.BLUE, "BLUE");

colors.put(Color.CYAN, "CYAN");

colors.put(Color.DARK_GRAY, "DARK_GRAY");

colors.put(Color.GRAY, "GRAY");

colors.put(Color.GREEN, "GREEN");

colors.put(Color.LIGHT_GRAY, "LIGHT_GRAY");

colors.put(Color.MAGENTA, "MAGENTA");

colors.put(Color.ORANGE, "ORANGE");

colors.put(Color.PINK, "PINK");

colors.put(Color.RED, "RED");

colors.put(Color.WHITE, "WHITE");

colors.put(new Color(192, 0, 255), "PURPLE");

colors.put(new Color(0xBADA55), "BADASS_GREEN");

colors.put(new Color(0, 0, 128), "DARK_BLUE");

Then, create a new Color out of the RGB values you have:

Color color = new Color(var1, var2, var3);

Last, get the value in colors for the key color:

String colorName = colors.get(color);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaColor是用来表示颜色,它包含了RGB(Red,Green,Blue)三种颜色的值以及透明度的值。以下是一些Color的使用示例: 1. 创建一个Color对象: ```java Color red = new Color(255, 0, 0); // 创建一个红色的Color对象 Color green = new Color(0, 255, 0); // 创建一个绿色的Color对象 Color blue = new Color(0, 0, 255); // 创建一个蓝色Color对象 ``` 2. 获取Color对象的RGB值: ```java Color red = new Color(255, 0, 0); // 创建一个红色的Color对象 int redValue = red.getRed(); // 获取红色的值,返回 255 int greenValue = red.getGreen(); // 获取绿色的值,返回 0 int blueValue = red.getBlue(); // 获取蓝色的值,返回 0 ``` 3. 修改Color对象的RGB值: ```java Color red = new Color(255, 0, 0); // 创建一个红色的Color对象 red = red.brighter(); // 使红色变亮一些 int redValue = red.getRed(); // 获取红色的值,返回 255 int greenValue = red.getGreen(); // 获取绿色的值,返回 51 int blueValue = red.getBlue(); // 获取蓝色的值,返回 51 ``` 4. 使用Color对象绘制图形: ```java import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class ColorExample extends JPanel { public void paint(Graphics g) { super.paint(g); setBackground(Color.WHITE); // 设置背景颜色为白色 g.setColor(Color.RED); // 设置画笔颜色为红色 g.fillRect(10, 10, 100, 100); // 绘制一个红色的矩形 } public static void main(String[] args) { JFrame frame = new JFrame("Color Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new ColorExample()); frame.setSize(250, 200); frame.setVisible(true); } } ``` 这个例子创建了一个窗口并在窗口绘制了一个红色的矩形。注意,使用Color对象绘制图形时要先设置画笔颜色

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值