Hi,
I'm using the getRGB function to get the color of a pixel of a BufferedImage.
It returned a number like 2001231.
What I want to do is use setRGB to set the color of this pixel to something so that getRGB returns 2000000 instead of the original value.
How will I define the color for the setRGB method based on what the getRGB returned originally (2001231), so setRGB sets 2000000?

Thanks

## All 7 Replies

It is more usefull to see and work with a hexidecimal display of the RGB values instead of a decimal display.
You will have to break the value 2000000 into its R G and B components so you can use them with the setRGB() method.

define the color for the setRGB method based on what the getRGB returned originally

Explain the relationship between the value returned by getRGB() and the value you want to use witht the setRGB() method.

Hi,
I tried using setRGB(x,y,3000000)
This is the result:
original color getRGB(x,y): 3026478
new color setRGB(x,y,3000000): 3000000
Color returned soon after setRGB was done (retrieved by getRGB(x,y)): -5066062
The getRGB must return 3000000 this time...
I don't know what I'm doing wrong here. May be something with alpha or something.

Thanks

Hi,
I tried using setRGB(x,y,3000000)
This is the result:
original color getRGB(x,y): 3026478
new color setRGB(x,y,3000000): 3000000
Color returned soon after setRGB was done (retrieved by getRGB(x,y)): -5066062
The getRGB must return 3000000 this time...
I don't know what I'm doing wrong here. May be something with alpha or something.

I got the r,g,b values in seperate variables as well, but don't know how to round them up to the 3000000 figure in this example, & then use new values with setRGB.
Some code to round them up will be useful.
Thanks

I got it with this:
setRGB(x,y, new Color(r,g,b).getRGB() )
Thanks

Hello,
I'm getting confused with this
Using the java.awt.Color class I made a color:

Color color= new Color( 255, 0, 0, 0 )

I set this color in my image at x,y using setRGB(x,y,color.getRGB())
Now when I try to getRGB of x,y the results are not as expected.
System.out.println( ((img.getRGB(i,j) >> 16) & 0xFF) +","+ ((img.getRGB(i,j) >> 8) & 0xFF) +","+ ((img.getRGB(i,j) & 0xFF) +" "+ color )

The result of above is
255,255,255 java.awt.Color[r=255,g=0,b=0]

It should be
255,0,0 java.awt.Color[r=255,g=0,b=0]

Why is it not setting the values of green & blue as 0, or am I retrieving them wrong?