Java – how to set the R, G, B and alpha components of color?

There are three integer values that make up the RGB value, and I also have the alpha component value of the color How do I set these 4 values to get the desired color

Solution

You can create a color object (these values should be ints between 0 and 255 or floats between 0f-1f):

Color c = new Color(red,green,blue,alpha);

If you want to draw an image with this color:

BufferedImage image = new BufferedImage(300,200,BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.getGraphics(); 
graphics.setColor(c);
graphics.fillRect(50,50,100,100);
graphics.dispose();

If you want to set only one pixel (the color model must be ARGB):

image.setRGB(50,c.getRGB());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>