Odd behaviour when using copy()

I create a PGraphics object pg1 and set its color to blue.
Then I want to copy a tile from pg1 to a PImage object the size of 1 tile.
I use pImg.copy(src, sx, sy, sw, sh, dx, dy, dw, dh); This should - if I understood the function correctly - copy from the src to pImg.

Here’s the code:

PGraphics pg1;
PImage[] imgTiles;
int tiles, tileDim;

void setup() {
  size(800, 800, P2D);

  tiles = 4;
  tileDim = int(width/tiles);
  imgTiles = createTiles();

PImage[] createTiles() { 

  pg1 = createGraphics(800, 800, P2D);

  pg1.background(0, 0, 255);

  PImage[] imgArray = new PImage[1];

  PImage tempImg = createImage(tileDim, tileDim, RGB);
  tempImg.copy(pg1, 0, 0, tileDim, tileDim, 0, 0, tileDim, tileDim);
  imgArray[0] = tempImg;
  return imgArray;

void draw() {
  image(imgTiles[0], 0, 0);

This shows a black tile, instead of a blue tile?

1 Like

I’ve found the answer! Rubberduck debugging wins again :slight_smile:

I have to replace pg1 with a PImage object, copy doesn’t work with a PGraphics object (in this case)

1 Like

Do you own a duck? :slight_smile:

Thanks for sharing!