Clear() not working properly

Hi All,

I’ve got this code:

PGraphics myline;
PGraphics myrect;
void setup(){
background(#FFFFFF);
size(600,600);
myline=createGraphics(width,height);
myrect=createGraphics(width,height);
}
void draw(){
  myrect.beginDraw();
  myrect.rect(50,50,198,198);
  myrect.endDraw();
  myline.beginDraw();
  myline.line(147,475,mouseX,mouseY);
  myline.endDraw();  
  image(myrect,0,0);
  image(myline,0,0);  
}
void mousePressed()
{
  myline.beginDraw();
  myline.clear();
  myline.endDraw();
}

What I want to do is clear all the lines as I press the mouse. The problem is only those part of the lines will be deleted which are above the rect. What should I do?

2 Likes

Hey There!

The lines are being deleted ! But because your not updating the background the pixels are not being refreshed and what was present before still stays.

Before everything in draw() just put background( 255,0,0 )
where ( 0 , 0 , 0 ) corresponds to RGB and the showcase of each color intensity.

Also a note to add the rectangle your drawing wouldn’t it be drawn over and over in draw() because your calling beginDraw() on it inside draw() ? It seems with your rectangle it be way more efficient to only draw it once if it not going to move to save resources.

2 Likes