11

There are 3 integer values that makes up a RGB value, and also i have the Alpha component value of the color. how do i set these 4 values to get the desired colour

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50
Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

22

You can create a Color object (the values should either be ints between 0-255 or floats between 0f-1f:

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

If you want to paint an image with that 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 only want to set a pixel (color model must be ARGB):

image.setRGB(50, 50, c.getRGB());
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • How do i change the already set RGBA values?. Is there a `c.setRGB(red,green,blue,alpha)` function? – Brethlosze Mar 29 '15 at 08:04
  • `setRGB(x, y, rgba)` takes an integer composite of red, green, blue and _alpha_. Create the color with transparency using `rgba = new Color(r, g, b, alpha).getRGB()` – dacwe Mar 31 '15 at 06:21
0

you can use :

panel1.setBackground(new Color(0.0f, 0.0f, 0.0f,0.5f));
Yasmine El
  • 17
  • 6
-1

you can also use

int colorToSet = Color.argb(alpha, red, green, blue); to set Alpha
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300