# Which instance actually executes beginDraw() and endDraw()?

**URL:** <https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057>\
**Category:** Coding Questions\
**Created:** [May 23, 2023, 6:43am UTC](https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057 "2023-05-23T06:43:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjwada](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@vjwada](https://discourse.processing.org/u/vjwada)\
**Post date:** [May 23, 2023, 6:43am UTC](https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057/1 "2023-05-23T06:43:22Z")

</div>

I want to have two PGraphics instances and create an image for each separately.

I wanted to prevent the creation of two different images due to changes in the argument contents during a long draw() execution, so I applied the same drawing method to two PGraphics instances in succession, but two different An image will be created.

In the method draws() below, the right and left halves of the image are intended to alternate between light and dark slowly, but the result is not as it should be.

```auto
PGraphics pgL,pgR;
int i = 0;
boolean f = true;

void setup()
{
  size(1000, 500, P2D);
  background(0);
  pgL = createGraphics(500, 500, P3D);
  pgR = createGraphics(500, 500, P3D);
}

void draw(){
  if(f == true){
    draws(i);
    i++;
    if(i==255) f = false;
  } else {
    draws(i);
    i--;
    if(i==0) f = true;
  }
  image(pgL, 0, 0);
  image(pgR, 500, 0);
}

void draws(int i)
{
  pgL.beginDraw();
  pgR.beginDraw();
  pgL.background(i);
  pgR.background(i);
  pgL.endDraw();
  pgR.endDraw();
}

```

If I change the inside of the method draws() as follows, it works as intended.

```auto
void draws(int i)
{
  pgL.beginDraw();
  pgL.background(i);
  pgL.endDraw();
  pgR.beginDraw();
  pgR.background(i);
  pgR.endDraw();
}

```

If I want to execute the methods beginDraw (), endDraw () for different PGraphics instances, do I still have to execute beginDraw () after endDraw ()?

Do I need to execute a method inside a PApplet instance, for example, to successively execute beginDraw () or other drawing methods?

Where can I find a tutorial on such coding?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 23, 2023, 9:01am UTC](https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057/2 "2023-05-23T09:01:29Z")

</div>

Welcome to the forum.

Although I can’t find it in the documentation it seems that you can’t nest beginDraw and endDraw which is why your first effort failed and the second succeeded.

If I understood the problem correctly then this modified sketch will provide a better solution. Not only does the backgrounds fade correctly but each image has the same rotating box.

```auto
PGraphics pgL, pgR;
int i = 0;
float ang = 0;
boolean f = true;

void setup() {
  size(1000, 500, P2D);
  background(0);
  pgL = createGraphics(500, 500, P3D);
  pgR = createGraphics(500, 500, P3D);
}

void draw() {
  if (f == true) {
    i++;
    if (i==255) f = false;
  } else {
    i--;
    if (i==0) f = true;
  }
  draws(pgL, i);
  draws(pgR, i);
  image(pgL, 0, 0);
  image(pgR, 500, 0);
}

void draws(PGraphics pg, int i) {
  pg.beginDraw();
  pg.background(i);
  pg.translate(pg.width/2, pg.height/2);
  pg.rotateY(ang);
  ang+=0.01;
  pg.fill(200, 255, 200);
  pg.box(100);
  pg.endDraw();
}

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [May 23, 2023, 6:33pm UTC](https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057/3 "2023-05-23T18:33:58Z")

</div>

Except that you shouldn’t update the angle `ang+=0.01;` inside the `draws()` function otherwise the two images will be at slightly different angles. Move the `ang` update back out to `draw()` instead so it only gets called once per frame.

This is similar to the code I used for stereo imaging: [https://discourse.processing.org/t/cross-eyed-stereoscopy/38637](https://discourse.processing.org/t/cross-eyed-stereoscopy/38637).

---

<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:** [May 23, 2023, 10:05pm UTC](https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057/4 "2023-05-23T22:05:55Z")

</div>

> [@scudly](#):
>
> Except that you shouldn’t update the angle `ang += 0.01;`

Optionally multiply [_frameCount_](https://processing.org/reference/frameCount.html) by the angle step:  
`final float ang = frameCount * .01; pg.rotateY(ang);`  
or just `pg.rotateY(frameCount * .01);`

---

<div class="post-metadata">

**Author:** ![vjwada](https://avatars.discourse-cdn.com/v4/letter/v/f14d63/32.png) [@vjwada](https://discourse.processing.org/u/vjwada)\
**Post date:** [May 24, 2023, 9:57am UTC](https://discourse.processing.org/t/which-instance-actually-executes-begindraw-and-enddraw/42057/5 "2023-05-24T09:57:56Z")

</div>

Thank you for your quick reply!  
Certainly what I want to implement is stereo imaging.

Eventually, I wanted to make it an Android app, but I couldn’t find a way to turn off the posture sensor on the device, so I started making it with a full scratch.

I’ve read the source code for PGraphics and PApplet, the actual resources for createGraphics are in PApplet, not PGraphics, so we can’t separate the beginDraw and endDraw combination in one PApplet instance in one sketch.

For these reasons, I have tried to create multiple PApplet instances and then create a PGraphics instance for each PApplet instance, but I have not succeeded yet.

Thank you.
