# Odd behaviour when using copy()

**URL:** https://discourse.processing.org/t/odd-behaviour-when-using-copy/12375
**Category:** Coding Questions
**Created:** [June 29, 2019, 9:14am UTC](https://discourse.processing.org/t/odd-behaviour-when-using-copy/12375 "2019-06-29T09:14:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lsk](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@lsk](https://discourse.processing.org/u/lsk)
#### Post date: [June 29, 2019, 9:14am UTC](https://discourse.processing.org/t/odd-behaviour-when-using-copy/12375/1 "2019-06-29T09:14:02Z")

</div>

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:

```auto
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);

  //blue
  pg1.beginDraw();
  pg1.background(0, 0, 255);
  pg1.endDraw();

  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?

---

<div class="post-metadata">

### Author: ![lsk](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@lsk](https://discourse.processing.org/u/lsk)
#### Post date: [June 29, 2019, 11:45am UTC](https://discourse.processing.org/t/odd-behaviour-when-using-copy/12375/2 "2019-06-29T11:45:28Z")

</div>

I’ve found the answer! Rubberduck debugging wins again 🙂

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 29, 2019, 1:26pm UTC](https://discourse.processing.org/t/odd-behaviour-when-using-copy/12375/3 "2019-06-29T13:26:54Z")

</div>

> [@lsk](#):
>
> Rubberduck debugging

Do you own a duck? 🙂

> **[Rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging)**
>
> In software engineering, rubber duck debugging (or rubberducking) is a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line by line, to the duck. Many other terms exist for this technique, often involving different (usually) inanimate objects, or pets such as a dog or a

Thanks for sharing!
