How to draw pixel at specific location on the buffer?

Yes, it will render the array which you finished with pi.updatePixels(); placing it on the screen.
The pixel array in the PImage contains the color of the pixels which are integers.
It’s good for applying filters etc. However with a PGraphics buffer you can use all drawing methods and also change the value of a single pixel using set()
The two codes below do the same thing, setting the pixel at location x=3 y=5 to red.

PImage pi;

void setup() {
  size(150, 150);
  pi = createImage(32, 32, RGB); 
  pi.loadPixels();
  for (int y = 0; y < pi.height; y++ ) { 
    for (int x = 0; x < pi.width; x++ ) { 
      int loc = x + y * pi.width; 
      if (x == 3 && y == 5) {
        pi.pixels[loc] = color(255, 0, 0);
      }
    }
  }
  pi.updatePixels();
  image(pi, 0, 0);
}

//-----------------------------------

PGraphics pg;

void setup(){
  size(150, 150);
  pg = createGraphics(32, 32); 
  pg.beginDraw();
  pg.background(0);
  pg.set(3, 5, color(255, 0, 0));
  pg.endDraw();
  image(pg, 0, 0);
}
3 Likes