10

I am using setRGB() for changing the values of the pixel of an image.

int rgb=new Color(0,0,0).getRGB();
image1.setRGB(i,j,rgb); //where i,j is the boundaries of the image

Here,i am setting all the pixel values with white. But the change is not getting reflected in the image. Any One knows about the setRGB() how it works?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rohit
  • 635
  • 6
  • 12
  • 22

2 Answers2

25

White is in RGB 255,255,255 so:

Color myWhite = new Color(255, 255, 255); // Color white
int rgb = myWhite.getRGB();

try {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("bubbles.bmp"));
    }
    catch (IOException e) {
    }

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            img.setRGB(i, j, rgb);
        }
    }

    // retrieve image
    File outputfile = new File("saved.png");
    ImageIO.write(img, "png", outputfile);
}
catch (IOException e) {
}
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Lo Juego
  • 1,305
  • 11
  • 12
  • If this answered your question, please accept it as the answer so the question gets marked as answered. – JeffC Nov 03 '15 at 21:03
2
 Color col = new Color(newValue, newValue, newValue);
            image1.setRGB(i, j, col.getRGB());
Lê Quang Duy
  • 767
  • 7
  • 14