Why I cant clear all the drawing??:(

Hi everyone, I am a beginner of processing and I have some questions. I have tried on using processing to draw and added a function to clear all drawing. When i press 'v', the button that i set for clear all, the background clean but when I start to draw new lines, the last stroke appear again. I cant really clear the whole drawing. Can u please help me to solve this? Thx for your time n thx for helping!!

int P = 0;
int A, B, C, D  = 0;
int prev_P =2;
int sx, sy, ex, ey;
boolean ready = false;
int SW =2;
PGraphics pg;

void setup() 
{
  size(600,600);
  background(51);
  pg = createGraphics(width,height); 
  pg.beginDraw();
  pg.background(51);
   pg.endDraw();  
}

void draw() 
{
  if (ready == true) {
    image(pg,0,0);
    switch (P) {
      case 0: drawLine(); break;
      case 1: drawRect(); break;
      case 2: drawCircle(); break;
    }
  } 
}

void drawLine() {
  stroke(A,B,C);
  strokeWeight(SW);
  line(sx,sy,ex,ey);
  }


void drawCircle() {
  fill(D);
  stroke(A,B,C);
  strokeWeight(SW);
  float r = sqrt((ex-sx)*(ex-sx)+(ey-sy)*(ey-sy));
  ellipseMode(CENTER);
  ellipse(sx,sy,2*r, 2*r);
}

void drawRect() {
  noFill();
  stroke(A,B,C);
  strokeWeight(SW);
  rectMode(CORNER);
  rect(sx,sy,2*(ex-sx),2*(ey-sy),15);
}

void updatepg() {
 pg.beginDraw();
  switch (P) {
  case 0: if (ready) pg_drawLine(); break;
  case 1: if (ready) pg_drawRect(); break;
  case 2: if (ready) pg_drawCircle(); break;
  }
  pg.endDraw();

  image(pg,0,0);
}

void pg_drawLine() { 
  pg.stroke(A,B,C);
  pg.strokeWeight(SW);
  pg.line(sx,sy,ex,ey);

}

void pg_drawCircle() {
  pg.fill(D);
  pg.stroke(A,B,C);
  pg.strokeWeight(SW);
  float r = sqrt((ex-sx)*(ex-sx)+(ey-sy)*(ey-sy));
  pg.ellipseMode(CENTER);
  pg.ellipse(sx,sy,2*r, 2*r);
}

void pg_drawRect() {
  pg.noFill();
  pg.stroke(A,B,C);
  pg.strokeWeight(SW);
  pg.rectMode(CORNER);
  pg.rect(sx,sy,2*(ex-sx),2*(ey-sy),15);
}

void keyPressed() {
   if (key == 'v' || key == 'V'){
    ready = false;
     background(51);
     pg = createGraphics(width,height); 
  pg.beginDraw();
  pg.background(51);
   pg.endDraw();  
  }
  if (key == ' ') {
    prev_P = P;
    P = P +1;
    P = P%3;
    ready = false;
    println(prev_P+": "+P);
  }

  if (key == 'z' || key == 'Z') {
    SW = SW +2; 
    if (SW > 60) {
      SW = 60;
    }
    ready = true;
  }

  if (key == 'x' || key == 'X') {
    SW = SW -1;
    if (SW < 1) {
      SW = 1;
    }
    ready = true;
  }

  if ( key == '1') {
    A = 0;
    B = 0;
    C = 0;
 D = 0; }
    if ( key == '2') {
      A = 179;
      B = 255;
      C = 0; 
    D = 153; }
      if ( key == '3') {
        A = 0;
        B = 0;
        C = 355;
      D = 205;}
        if ( key == '4') {
          A = 230;
          B = 125;
          C = 0; 
        D = 255;}
         if ( key == '5') {
          A = 51;
          B = 51;
          C = 51; 
        D = 0;}
        if ( key == 'm' || key == 'M' ) {
          pg.clear();
        } 
  if ( key == 's' || key == 'S') {
    saveFrame("images/artwork-####.png");
  }
}

void mousePressed() {
  int cur_P = P;
  if (P != prev_P) {
    P = prev_P;
  }
  ready = true; 
  updatepg();
  sx = mouseX;
  sy = mouseY;
  if (P != cur_P) {
    P = cur_P;
    prev_P = P;
  }
  ready = false;
}

void mouseDragged() {
  ex = mouseX;
  ey = mouseY;
  ready = true;
}
Tagged:

Answers

  • It is the way you are calling your functions. Add the folling line inside your V block in keyPressed:

    sx=ex;
    sy= ey;
    

    Now, when you clear, you don't have a previous line but a dot... You need to change your logic a bit there in your draw function.

    Kf

Sign In or Register to comment.