# P2D vs JAVA2D - strange differences in PGraphics methods and masking

**URL:** https://discourse.processing.org/t/p2d-vs-java2d-strange-differences-in-pgraphics-methods-and-masking/11853
**Category:** Processing.py
**Created:** [June 5, 2019, 2:55am UTC](https://discourse.processing.org/t/p2d-vs-java2d-strange-differences-in-pgraphics-methods-and-masking/11853 "2019-06-05T02:55:39Z")
**Posts on this page:** 3
**Page:** 2

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [June 7, 2019, 1:32pm UTC](https://discourse.processing.org/t/p2d-vs-java2d-strange-differences-in-pgraphics-methods-and-masking/11853/21 "2019-06-07T13:32:04Z")

</div>

> [@GoToLoop](#):
>
> But it’s pretty useless given we need to use a `try {} / catch () {}` block for it, even though it’s merely a **get()** alias:

Yes, and breaks semantics for `clone()` - just pointing out that you don’t really want what you say you want! 😉

> [@GoToLoop](#):
>
> Here’s my attempt of cloning a PGraphics …

Yes, definitely broken in 3.5.3. In fact, looks like some other uses of the pixel array are broken too. Going to do a diff on the sources.

However, creating a copy of a PGraphics via the pixel array is a daft idea anyway, particularly if the graphics is on the GPU. Just do this, which works correctly across renderers, and will perform vastly better -

```auto
static final String RENDER = P3D, URL =
  "https://" + "Forum.Processing.org/" + "processing-org.jpg";

PImage img;
PGraphics clonedPg;

void settings() {
  img = loadImage(URL);
  size(img.width, img.height, RENDER);

  smooth(8);
  noLoop();
}

void setup() {
  // Buggy for all renderers but JAVA2D under PDE 3.5.3:
  final PGraphics pg = createGraphics(width, height, RENDER);

  pg.beginDraw();
  pg.blendMode(REPLACE);
  pg.image(img, 0, 0);
  pg.endDraw();

  clonedPg = clonePGraphics(pg);
}

void draw() {
  image(clonedPg, 0, 0);
}

PGraphics clonePGraphics(final PGraphics pg) {
  final int w = pg.width, h = pg.height;
  final PGraphics cloned = createGraphics(w, h, RENDER);

  cloned.beginDraw();
  cloned.blendMode(REPLACE);
  cloned.image(pg, 0, 0);
  cloned.endDraw();

  return cloned;
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 7, 2019, 4:07pm UTC](https://discourse.processing.org/t/p2d-vs-java2d-strange-differences-in-pgraphics-methods-and-masking/11853/22 "2019-06-07T16:07:54Z")

</div>

> [@neilcsmith](#):
>
> `cloned.blendMode(REPLACE);`

You should set the **blendMode()** back to its default BLEND at the end: ⚠

> **[blendMode() / Reference](https://processing.org/reference/blendMode_.html)**
>
> Blends the pixels in the display window according to a defined mode. There is a choice of the following modes to blend the source pixels (A) with the ones of pixels already in the display window (B). …

```auto
cloned.beginDraw();
cloned.blendMode(REPLACE);
cloned.image(pg, 0, 0);
cloned.blendMode(BLEND);
cloned.endDraw();

```

Also, `cloned.image(pg, 0, 0);` unnecessarily stores the original PGraphics object _pg_ in _cloned_’s internal WeakHashMap. #️⃣

Therefore the _cloned_ PGraphics object isn’t fully detached from the original 1. 🙄

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [June 7, 2019, 4:27pm UTC](https://discourse.processing.org/t/p2d-vs-java2d-strange-differences-in-pgraphics-methods-and-masking/11853/23 "2019-06-07T16:27:47Z")

</div>

> [@GoToLoop](#):
>
> You should set the **blendMode()** back to its default BLEND at the end:

Yes, good point!

> [@GoToLoop](#):
>
> Also, `cloned.image(pg, 0, 0);` unnecessarily stores the original PGraphics object _pg_ in _cloned_ ’s internal WeakHashMap.

a) who cares?!  
b) no, it doesn’t! (well, with this renderer anyway) 😀

[Previous page](https://discourse.processing.org/t/p2d-vs-java2d-strange-differences-in-pgraphics-methods-and-masking/11853.md?page=1)
