# PGraphics with "for"

**URL:** https://discourse.processing.org/t/pgraphics-with-for/32917
**Category:** Coding Questions
**Created:** [October 19, 2021, 12:26am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917 "2021-10-19T00:26:02Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)
#### Post date: [October 19, 2021, 12:26am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/1 "2021-10-19T00:26:02Z")

</div>

Hi, I’m trying to convert some code to PGraphics because I want to save the result in a bigger size, but I’m using a “for” part, and I can’t find the right way to make that everything works like in the code without PGraphics.

I don’t find help in other topics of the forum, neither in the reference page.

My original code is:

```auto
float t=1;
void setup() {
  size(900, 900);
}

void draw() {
  background(#FFFFFF);
  fill(#000000);
  translate(width/4, height/4);
  for (int i = 0; i < 50; i++) {
    float x1 = i;
    float y2 = t*i*4;
    ellipse(x1, y2, 10, 10);
  }

  t += 0.01;

```

And I thought this would work, but it isn’t:

```auto
PGraphics pg;

float t=1;

void setup() {
  size(900, 900);
  pg=createGraphics(width, height);
}

void draw() {
  pg.beginDraw();
  pg.background(#FFFFFF);
  pg.fill(#000000);
  pg.translate(width/4, height/4);
  for (int i = 0; i < 50; i++) {
    float x1 = i;
    float y2 = t*i*4;
    pg.ellipse(x1, y2, 10, 10);
    pg.endDraw();

    image(pg, 0, 0);
  }
  t += 0.01;

```

Anyone can help me?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 19, 2021, 12:53am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/2 "2021-10-19T00:53:51Z")

</div>

Hello,

```auto
void draw() 
  {
  pg.beginDraw();
  pg.background(#FFFFFF);
  pg.fill(#000000);
  pg.translate(width/4, height/4);
  for (int i = 0; i < 50; i++) 
    {
    float x1 = i;
    float y2 = t*i*4;
    pg.ellipse(x1, y2, 10, 10);
    }
  pg.endDraw();
  image(pg, 0, 0);
  
  t += 0.01;
  }

```

`:)`

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 19, 2021, 1:00am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/3 "2021-10-19T01:00:22Z")

</div>

Hi

You have same error in both codes

```auto
float t=1;
void setup() {
  size(900, 900);
}

void draw() {
  background(#FFFFFF);
  fill(#000000);
  translate(width/4, height/4);
  for (int i = 0; i < 50; i++) {
    float x1 = i;
    float y2 = t*i*4;
    ellipse(x1, y2, 10, 10);
 t += 0.01;
  }
  }

// t += 0.01;

```

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 19, 2021, 1:01am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/4 "2021-10-19T01:01:58Z")

</div>

```auto
PGraphics pg;

float t=1;

void setup() {
  size(900, 900);
  pg=createGraphics(width, height);
}

void draw() {
  pg.beginDraw();
  pg.background(#FFFFFF);
  pg.fill(#000000);
  pg.translate(width/4, height/4);
  for (int i = 0; i < 50; i++) {
    float x1 = i;
    float y2 = t*i*4;
    pg.ellipse(x1, y2, 11, 10);
    pg.endDraw();
    image(pg, 0, 0);
t += 0.01;
 }
  }
 // t += 0.01;

```

---

<div class="post-metadata">

### Author: ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)
#### Post date: [October 19, 2021, 1:20am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/5 "2021-10-19T01:20:29Z")

</div>

Thank you very much, glv, it was so easy! (but not for me)

---

<div class="post-metadata">

### Author: ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)
#### Post date: [October 19, 2021, 1:22am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/6 "2021-10-19T01:22:24Z")

</div>

jafal, thank you but it doesn’t work like I expected, with the code of glv it’s perfect for me, so I’m not sure if that was an error or a different way to do it

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [October 19, 2021, 1:29am UTC](https://discourse.processing.org/t/pgraphics-with-for/32917/7 "2021-10-19T01:29:53Z")

</div>

@Karrajo yes glv way is correct

```auto
pg.ellipse(x1, y2, 10, 10);
    }
  pg.endDraw();
  image(pg, 0, 0);
  
  t += 0.01;
  }

```
