Need Help!! Pgraphics Clear() does not work well in my code..

edited December 2015 in Questions about Code

Hello, I'm new here and very pleasured to be at a minimun level to ask a question understanding Processing codes (>.<)V. I'm making an interactive drawing tool to save one's own alphabet types(.png) with a transparent background. But I have a problem to clear the previous type to draw a new type after using PGrphics... I've found lots of references for this but I couldn't find a solution to clean the type written.

Maybe for someone it's a simple to solve this problem, I hope your help please~!!

Thank you for your help in advance :)


Here's my code for it :

PGraphics alphaG;

float med_mouseX=mouseX;

float med_mouseY=mouseY;

void setup() {

size(400, 500); background(255); smooth(); frameRate(30); alphaG = createGraphics(width, height); }

void draw() { alphaG.beginDraw();

if (mousePressed) {

//draw an alphabet type
//alphaG.noStroke();
med_mouseX=mouseX+random(-5, 30);
med_mouseY=mouseY+random(-5, 30);
alphaG.stroke(255);
alphaG.strokeWeight(random(20, 60));
alphaG.strokeCap(ROUND);
alphaG.stroke(0, random(0, 255), random(0, 255));
alphaG.line(mouseX, mouseY, pmouseX, pmouseY);
alphaG.endDraw();
image(alphaG, 0, 0);

} }

void keyPressed() { //clear a type written if (key == BACKSPACE) { alphaG.clear(); } //save an alpha written with keyboard(this code can be shorter? ^^?) if (key=='a') { String fileName = "data/"+"a"+"1"+".png"; alphaG.save(fileName); }

if (key=='b') { String fileName = "data/"+"b"+"1"+".png"; alphaG.save(fileName); }

if (key=='c') { String fileName = "data/"+"c"+"1"+".png"; alphaG.save(fileName); }

if (key=='d') { String fileName = "data/"+"d"+"1"+".png"; alphaG.save(fileName); }

if (key=='e') { String fileName = "data/"+"e"+"1"+".png"; alphaG.save(fileName); }

if (key=='f') { String fileName = "data/"+"f"+"1"+".png"; alphaG.save(fileName); }

if (key=='g') { String fileName = "data/"+"g"+"1"+".png"; alphaG.save(fileName); }

if (key=='h') { String fileName = "data/"+"h"+"1"+".png"; alphaG.save(fileName); }

if (key=='i') { String fileName = "data/"+"i"+"1"+".png"; alphaG.save(fileName); }

if (key=='j') { String fileName = "data/"+"j"+"1"+".png"; alphaG.save(fileName); }

if (key=='k') { String fileName = "data/"+"k"+"1"+".png"; alphaG.save(fileName); }

if (key=='l') { String fileName = "data/"+"l"+"1"+".png"; alphaG.save(fileName); }

if (key=='m') { String fileName = "data/"+"m"+"1"+".png"; alphaG.save(fileName); }

if (key=='n') { String fileName = "data/"+"n"+"1"+".png"; alphaG.save(fileName); }

if (key=='o') { String fileName = "data/"+"o"+"1"+".png"; alphaG.save(fileName); }

if (key=='p') { String fileName = "data/"+"p"+"1"+".png"; alphaG.save(fileName); }

if (key=='q') { String fileName = "data/"+"q"+"1"+".png"; alphaG.save(fileName); }

if (key=='r') { String fileName = "data/"+"r"+"1"+".png"; alphaG.save(fileName); }

if (key=='s') { String fileName = "data/"+"s"+"1"+".png"; alphaG.save(fileName); }

if (key=='t') { String fileName = "data/"+"t"+"1"+".png"; alphaG.save(fileName); }

if (key=='u') { String fileName = "data/"+"u"+"1"+".png"; alphaG.save(fileName); }

if (key=='v') { String fileName = "data/"+"v"+"1"+".png"; alphaG.save(fileName); }

if (key=='w') { String fileName = "data/"+"w"+"1"+".png"; alphaG.save(fileName); }

if (key=='x') { String fileName = "data/"+"x"+"1"+".png"; alphaG.save(fileName); }

if (key=='y') { String fileName = "data/"+"y"+"1"+".png"; alphaG.save(fileName); }

if (key=='z') { String fileName = "data/"+"z"+"1"+".png"; alphaG.save(fileName); } }

Answers

  • Answer ✓

    well, first image(alphaG, 0, 0); is inside the if-clause if (mousePressed) {

    Hence you see the image only when mouse is pressed. Do you want this behaviour? Same goes for alphaG.endDraw();

    second, you can use background() as well

    the save part is all a little long winded... you could make a file name from date and time (stamp)

    Best, Chrisir

  • Thank you for your help! Yes I want the image when mouse is pressed. Then it has to be saved with A to Z key of a keyboard. My tool works well now with your answer! For the save part, I need to save each alphabet image with the keyboard from A to Z. But the code for this part is too long. I'm studying now. Do you have any idea for it? :D Thanks again~!

    DongY

  • edited December 2015

    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

    PGraphics alphaG;
    
    void keyPressed() {
      final int k = keyCode;
    
      if (k >= 'A' & k <= 'Z')  alphaG.save(dataPath((char) (k + ' ') + "1.png"));
      else if (k == BACKSPACE)  alphaG.clear();
    }
    
Sign In or Register to comment.