# Scoped beginDraw and endDraw

**URL:** https://discourse.processing.org/t/scoped-begindraw-and-enddraw/21767
**Category:** Coding Questions
**Created:** [June 11, 2020, 11:08am UTC](https://discourse.processing.org/t/scoped-begindraw-and-enddraw/21767 "2020-06-11T11:08:39Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)
#### Post date: [June 11, 2020, 11:08am UTC](https://discourse.processing.org/t/scoped-begindraw-and-enddraw/21767/1 "2020-06-11T11:08:39Z")

</div>

is there any way to use PGraphics.beginDraw and PGraphics.endDraw outside of the scope of PGraphics.\*() calls?

for example if i do this then nothing renders

```auto
  void drawGraphics() {
    //graphics.endDraw();
    if (displayFPS) {
      window.graphics.beginDraw();
      int oldColor = window.graphics.fillColor;
      window.graphics.fill(255);
      window.graphics.textSize(16);
      window.graphics.text("FPS: " + frameRate, 10, 20);
      window.graphics.fill(oldColor);
      window.graphics.endDraw();
    }
    //graphics.beginDraw();
    graphics.image(
      window.graphics,
      window.x,
      window.y,
      window.width,
      window.height
    );
  }
  
  void drawWindow() {
    graphics.beginDraw();
    clearScreen();
    if (focus) {
      if (clickedOnBorder && locked) drawBordersLocked();
      else drawBordersHighlighted();
    } else {
      drawBorders();
    }
    drawGraphics();
    graphics.endDraw();
  }

```

but if i do this then it renders

```auto
  void drawGraphics() {
    graphics.endDraw();
    if (displayFPS) {
      window.graphics.beginDraw();
      int oldColor = window.graphics.fillColor;
      window.graphics.fill(255);
      window.graphics.textSize(16);
      window.graphics.text("FPS: " + frameRate, 10, 20);
      window.graphics.fill(oldColor);
      window.graphics.endDraw();
    }
    graphics.beginDraw();
    graphics.image(
      window.graphics,
      window.x,
      window.y,
      window.width,
      window.height
    );
  }
  
  void drawWindow() {
    graphics.beginDraw();
    clearScreen();
    if (focus) {
      if (clickedOnBorder && locked) drawBordersLocked();
      else drawBordersHighlighted();
    } else {
      drawBorders();
    }
    drawGraphics();
    graphics.endDraw();
  }

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [June 11, 2020, 6:24pm UTC](https://discourse.processing.org/t/scoped-begindraw-and-enddraw/21767/2 "2020-06-11T18:24:52Z")

</div>

> [@mgood7123](#):
>
> to use PGraphics.beginDraw and PGraphics.endDraw outside of the scope of PGraphics.\*() calls?

I don’t understand this question, in part because you have provided a code fragment and I don’t know what any of these variables are. What is “window”? What is “graphics”? What is “graphics.image()”?

In general, if you have a PGraphics object, beginDraw() initializes it for drawing – for example, allocating a buffer. endDraw() writes the buffer to the object’s internal storage. How this happens specifically depends on your renderer – e.g. JAVA2D, P2D, or P3D.

The most common problems are: if you try to draw to a PGraphics without calling beginDraw, either nothing happens or you get an error because there is no buffer. Or, if you don’t call endDraw, when you then display your PGraphics on the canvas none of your drawing is there – because you didn’t actually copy your buffer and commit your changes!
