# How to adapt my existing methods that write to the screen to work with PGraphics instead - when required?

**URL:** <https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609>\
**Category:** Coding Questions\
**Created:** [April 24, 2019, 9:41pm UTC](https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609 "2019-04-24T21:41:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlieCheeseCake](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@CharlieCheeseCake](https://discourse.processing.org/u/CharlieCheeseCake)\
**Post date:** [April 24, 2019, 9:41pm UTC](https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609/1 "2019-04-24T21:41:52Z")

</div>

I’m able to successfully draw to the screen using standard methods such as : `stroke, line & fill.`

Similarly I’m able to draw to a graphics buffer, thus:

```auto
PGraphics buff;
buff = createGraphics( 200, 200 );
buff.beginDraw();
buff.background( 0 );
buff.fill ......
buff.rect .....
buff.endDraw();

```

Both of those cases work fine. Well done me.

**My question is this** - How could I refactor my existing methods that draw to the _screen_ so that they are able to draw to the graphics buffer _when required_ ?

ie I want to achieve something like below but without the conditional, as that would requires me to double up all the drawing commands just to prefix them with `buff.` :

```auto
PGraphics buff;
buff = createGraphics( 200, 200 );

public void myMethodthatDrawsStuff( PGraphics buff, int x, int y, int w, int h ) {
    // this method would have multiple rects, lines, fills, strokes etc in reality

    if (buff == null)
        rect (x, y, w, h);
    else
        buff.rect (x, y, w, h);
}

```

I’m recalling the ‘with … do’ from Pascal… but I think we’ve all moved on a bit from those days 😉

Hope I could describe the issue adequately. Grateful for any assistance.  
Cheers!

---

<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:** [April 24, 2019, 10:01pm UTC](https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609/2 "2019-04-24T22:01:26Z")

</div>

[Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#getGraphics--](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#getGraphics--)

```java
// https://Discourse.Processing.org/t/
// how-to-adapt-my-existing-methods-that-write-to-the-screen-
// to-work-with-pgraphics-instead-when-required/10609/2

// 2019-04-24

PGraphics buff;

void setup() {
  size(300, 200);
  noLoop();

  background(#FFFF00);
  fill(#0000FF);

  myMethodThatDrawsStuff(width>>2, height>>3, width>>1, height>>2);

  buff = createGraphics(width, height>>1);

  buff.beginDraw();
  buff.background(#00FFFF);
  buff.fill(#FF0000);
  buff.endDraw();

  myMethodThatDrawsStuff(buff, width>>2, height>>3, width>>1, height>>2);
  set(0, height>>1, buff);
}

void myMethodThatDrawsStuff(int x, int y, int w, int h) {
  myMethodThatDrawsStuff(null, x, y, w, h);
}

void myMethodThatDrawsStuff(PGraphics pg, int x, int y, int w, int h) {
  if (pg == null) pg = getGraphics();

  pg.beginDraw();
  pg.rect(x, y, w, h);
  pg.endDraw();
}

```

---

<div class="post-metadata">

**Author:** ![CharlieCheeseCake](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@CharlieCheeseCake](https://discourse.processing.org/u/CharlieCheeseCake)\
**Post date:** [April 24, 2019, 10:14pm UTC](https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609/3 "2019-04-24T22:14:21Z")

</div>

Spot on. Thanks so much for a quik response @GoToLoop👍  
its getting late here - will test this out tomorrow for sure.

---

<div class="post-metadata">

**Author:** ![CharlieCheeseCake](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@CharlieCheeseCake](https://discourse.processing.org/u/CharlieCheeseCake)\
**Post date:** [April 25, 2019, 1:29pm UTC](https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609/4 "2019-04-25T13:29:11Z")

</div>

Just what I needed and makes total sense.  
Plus, I bonus-learned ‘set’ ‘\>\>’ and ‘noLoop()’  
Thanks again.

---

<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:** [April 29, 2019, 3:39pm UTC](https://discourse.processing.org/t/how-to-adapt-my-existing-methods-that-write-to-the-screen-to-work-with-pgraphics-instead-when-required/10609/5 "2019-04-29T15:39:21Z")

</div>

In general this solution that @GoToLoop shared (optional PGraphics, default canvas) is a great pattern for libraries that draw, to make them work with either a PGraphics or the main canvas. In the case of libraries that draw onto their own buffer (like a HUD) you can also use this pattern for the render method, or make it part of the constructor, so that the final composited output can be redirected to either the main canvas or a PGraphics in this way.

An example of that approach is Peasycam, which draws to the canvas by default but has an alternate constructor for an optional PGraphics.

[http://mrfeinberg.com/peasycam/reference/peasy/PeasyCam.html](http://mrfeinberg.com/peasycam/reference/peasy/PeasyCam.html)
