Using colors with variables

Hi again :slight_smile:

Instead of checking the last key that was pressed with you if statements: if (key == xxx) you should try using the keyPressed() function.

You get inside everytime the user press a key and this is where you can put all of your code. You just need to switch some variables to remember the mode you are using - random or fixed color.

Check this example:

PGraphics circles;
//Variables for the size and color of the ball and the text
boolean randomCol;
int size;


void setup() 
{
  //Setup the window
  size(1280, 730);
  background(255);
  fill(0);

  circles = createGraphics(1280, 730); 

  randomCol = false;
  size = 10;
}

void draw() 
{
  //Setup
  background(255);
  noStroke();
  noCursor();
  //When different keys are pressed they change the color of the ellipse
  color col;
  if (randomCol) {
    col = color(random(255), random(255), random(255));
  } else {
    col = color(0);
  }


  //If mouse is pressed, then draw 
  if (mousePressed)
  {
    circles.beginDraw();
    circles.noStroke();
    circles.fill(col);
    circles.ellipse(mouseX, mouseY, size, size);
    circles.endDraw();
  }
  //Print the second plane
  image(circles, 0, 0);
  //If mouse isn't pressed, show the ellipse but dont draw it
  fill(col);
  ellipse(mouseX, mouseY, size, size);
}


void keyPressed() {
  if (key == TAB)
  {
    randomCol = !randomCol;
  }
  //Press keys 0-9 to change size of the ellipse
  if (key == '1')
  {
    size = 10;
  }
  if (key == '2')
  {
    size = 20;
  }
  if (key == '3')
  {
    size = 30;
  }
}