Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

modifying a BufferedImage  RSS feed

Larry Dixon
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused. I have a JComponent whose paintComponent draws a BufferedImage.
The constructor just uses ImageIO to read the image ( .gif) from a file.
Each color in the image is mapped and known.
I have a mouse listener that detects a mouse click anywhere in the image, determines the what the color is, and is supposed to replace every pixel of that color with a highlight color.
I am using the following code:

private void resetColor(int oldColor, int newColor) {
log.debug("in resetColor, oldColor is: " + oldColor + " " + new Color(oldColor) + " newColor is: " + newColor + " " + new Color(newColor));
int i = 0;

for (int x = 0; x < image.getWidth(); ++x) {
for (int y = 0; y < image.getHeight(); ++y) {
if (image.getRGB(x, y) == oldColor) {
image.setRGB(x, y, newColor);
log.debug("image.getRGB("+x+","+y+") is: " + Integer.toHexString(image.getRGB(x, y)));
log.debug("out of resetColor changed " + i + " pixels.");

The pixels are replaced but as you can see above, I print the color that I am setting each pixel to, then I get the color that I set it to. The output of this shows that those colors are different! ( see below ) What am I doing wrong? Any help would be appreciated.

output from above code:

2005-08-08 16:00:02,871 [AWT-EventQueue-0] DEBUG - in resetColor, oldColor is: -4008511 java.awt.Color[r=194,g=213,b=193] newColor is: -1723983 java.awt.Color[r=229,g=177,b=177]
2005-08-08 16:00:02,871 [AWT-EventQueue-0] DEBUG - image.setRGB(92,126,ffe5b1b1)
2005-08-08 16:00:02,871 [AWT-EventQueue-0] DEBUG - image.getRGB(92,126) is: ffc2d4c0
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!