PGraphics with "for"

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:

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:

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?

Hello,

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;
  }

:)

Hi

You have same error in both codes

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;


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;

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

1 Like

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

2 Likes

@Karrajo yes glv way is correct

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