# Execute code on PGraphics

**URL:** https://discourse.processing.org/t/execute-code-on-pgraphics/43365
**Category:** Beginners
**Created:** [December 1, 2023, 6:19pm UTC](https://discourse.processing.org/t/execute-code-on-pgraphics/43365 "2023-12-01T18:19:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)
#### Post date: [December 1, 2023, 6:19pm UTC](https://discourse.processing.org/t/execute-code-on-pgraphics/43365/1 "2023-12-01T18:19:22Z")

</div>

Hi, i have a quesetion! I have a class:

```auto
class Thing {
int x = 0;
Thing() {
x = 50;
}
void create(int x_) {
this.x = x_;
}
void display() {
circle(width/2, height/2, x);
}
}

```

i also have a Pgraphics:

```auto
PGraphics ui;

void setup() {
size(960,540,P3D);
ui = createGraphics(100,100);
}
void draw() {
background(255);
image(ui, 0, 0);
}

```

I now want to make an instance of the Thing class. I want it to be displayed on top of the pgraphics. however i do not want to make the Thing class be dependent on the pgrahics, (every drawing in the class happens on the graphics). I want to be able to do something like this:

```auto
PGraphics ui;
Thing instance;

void setup() {
  size(960, 540, P3D);
  ui = createGraphics(100, 100);
  instance = new Thing();
  instance.create(100);
}
void draw() {
  background(255);
  image(ui, 0, 0);
  ui.beginDraw();
  ui.clear();
  ui.instance.display();
  ui.endDraw();
}

class Thing {
  int x = 0;
  Thing() {
    x = 50;
  }
  void create(int x_) {
    this.x = x_;
  }
  void display() {
    circle(width/2, height/2, x);
  }
}

```

Thank you for your patience!  
-Noodly

---

<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: [December 1, 2023, 8:55pm UTC](https://discourse.processing.org/t/execute-code-on-pgraphics/43365/2 "2023-12-01T20:55:53Z")

</div>

You have to pass the PGraphics that you want to use into your `display()` function. Also, `image()` the PGraphics after you draw to it, not before.

```auto
class Thing {
  void display( PGraphics pg ) {
    pg.circle( pg.width/2, pg.height/2 );
  }
}

void draw() {
  background(255);
  ui.beginDraw();
  ui.clear();
  instance.display( ui );
  ui.endDraw();
  image( ui, 0, 0 );
}

```
