Reputation: 1165
I am writing a small program in java which will draw a path on an image. To do so, I have the following code
while(!path.isEmpty())
{
Position p = path.poll();
image.setRGB(p.getX(),p.getY(),Color.red.getRGB());
}
Where path is a Queue of objects which old the X and Y coordinates and image is a standard BufferedImage (from ImageIO.read). This code is just meant to draw a red pixel on every pixel of the image that does is in the queue. Instead of Red though, when I write this image to file I get a gray color.
The return value of Color.red.getRGB is 0xFFFF0000. When I do a getRGB on the pixel after I set it to red, I get back 0xFF7F7F7F.
I am relatively new to Java and have no idea why this is happening. Any help would be greatly appreciated.
If it makes a difference, the image is from a .bmp file.
Upvotes: 1
Views: 558
Reputation: 9705
It could be that you're using a BufferedImage that's a grayscale type, or a type that maps those sRGB values to a gray color.
Generally, you have one of two possibilities:
Since you're loading the image from a .bmp file, the 2nd one is probably your case.
For info about color value conversion issues, see here and here.
Generally, if you just want to learn about image handling in Java, I would suggest to use the second BufferedImage constructor with *TYPE_INT_ARGB* as the type for starters, and expand your code from that. From what I remember of my early Java days, learning image loading can be a bit tricky :).
Also, you might want to read the official Java2D tutorial. It's a quite good introduction to the topic.
Upvotes: 2