• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

modifying a BufferedImage

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {
++i;
log.debug("image.setRGB("+x+","+y+","+Integer.toHexString(newColor)+")");
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
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic