Change to a specific color variation with a click of a specific button

edited December 2017 in Questions about Code

Hey, I'm trying to have the color change and still be able to move the mouse with a click of a specific key. After clicking the Key I want it to continue as the new color. I want I am also having it save as a transparent version with a click of a key as te code is now. It would be nice to have it be a specific button as well but I can figure out how to code it correctly. Also it resaves over the same name which I wish I could have it save as name 1 name 2, but when I added the code that works without transparency it didnt work with this version. I am a student so I'm not that experienced with this.

float rad;
float speed;

PGraphics alphaG;

// Example 3-5: mousePressed and keyPressed
void setup() {
  size(800, 800);
  // create an extra pgraphics object for rendering on a transparent background
alphaG = createGraphics(width,height, JAVA2D);
  background(0);
  frameRate(20);
  rad = 16;
  speed = 0;
}

void draw() {
  speed = abs (mouseX - pmouseX);

  println (speed); 
  //if mouse travels faster draw a smaller ellipse
 // if (){
  //};
}

// Whenever a user clicks the mouse the code written inside mousePressed() is executed.
void mouseDragged() {
 alphaG.beginDraw();
 alphaG.stroke(0,0,0,50);
  alphaG.fill(5+speed*5,150,5,50);
  alphaG.ellipseMode(CENTER);
  alphaG.ellipse(mouseX, mouseY, (rad + speed)*9, (rad + speed)*5);
  alphaG.endDraw();
   image(alphaG, 0,0);
}

// Whenever a user presses a key the code written inside keyPressed() is executed.
int x = 0;
void keyPressed() {
  alphaG.save("alpgaG.png"); 
  println("AlphaG.png saved.");
}
// stroke(0,0,0,50);
 // fill(5+speed*5,50,150,150);
 // ellipseMode(CENTER);
 // ellipse(mouseX, mouseY, (rad + speed)*9, (rad + speed)*5);
//}

Answers

  • edited December 2017

    I also had a version where I did change the color but then I couldn't move the mouse with that changed color and letting go of the button would change it back to the original color. But that code didnt have the save transparency which is more important.

  • Hey, I'm trying to have the color change and still be able to move the mouse with a click of a specific key

    Right now, when you press any key, you are only saving your PGraphics. You are not changing the color.

    Right now you are defining your color based on mouse speed. If you have a key action to make your program remember your color, then you have a conflict in your design as color is defined on speed.

    Also it resaves over the same name which I wish I could have it save as name 1 name 2, but when I added the code that works without transparency it didnt work with this version.

    You have two problems there. Try not to mix your problems as it confuses your problem. I cannot see the prob with transparency. Related to the file naming, what about this?

    int ctr=0;
    void keyPressed() {
      String fn="alpgaG"+(ctr++)+".png";
      alphaG.save(fn); 
      println(fn+" saved.");
    }
    

    One last thing: you should be calling image() in draw. Avoid calling it in the system events.

    Kf

Sign In or Register to comment.