0

I have a little problem. I am learning to program games in Java , and I watched a tutorial on using BufferedImage to acess each pixel by array, I mean :

int[] pixels = ((DataBufferInt) screen.getRaster().getDataBuffer()).getData();

It all works, but I do not understand it, when I pick all the pixels from that image and store them to array called pixels, then I just change color data in that array and it works like I changed the data in the actual image, and I didn't put anything from array back to the BufferedImage. Can you please explain it? Thanks

Jacob
  • 111
  • 1
  • 2
  • 8
  • 2
    Well, the BufferedImage itself has to store pixel data somewhere. That somewhere is in an array of pixels. With that code, you're getting that exact same array that the BufferedImage is using/wrapping, so when you modify it, you're also implicitly modifying the BufferedImage. – xs0 Nov 30 '17 at 17:11
  • so, I understand it like I am crreating main array for that image that is connected with it instead of storing the values somewhere. Thanks – Jacob Nov 30 '17 at 17:31
  • 1
    Well, you're not creating it, just getting a reference to the one that is already existing.. Look, an array is basically just a chunk of memory (RAM). When you do that code above, all that happens is that `pixels` becomes a pointer to that chunk of memory that the image was already using (and the BufferedImage inside is using a different pointer to the same chunk).. – xs0 Nov 30 '17 at 17:34
  • Ok, that is enough for me for now, thanks. – Jacob Nov 30 '17 at 17:38

0 Answers0