Stroke won't change colour when clicked

This is my painting program. It has a frame and colours at the bottom. I want the stroke to change to the colour of the image at the bottom when clicked. But it is not working. any suggestions? here is my code and images.

//Created by: Alessandra Macchione and Cristian Pertea
//Created on: May 20th, 2018
//Created for: Computer Science
//Final Assignment
 
PImage frame;
PImage red;
PImage orange;
PImage yellow;
PImage green;
PImage blue;
PImage purple;
PImage white;
PImage grey;
PImage black;
PImage eraser;
PImage brush;
 
//-------------------------------------------------------------------------
 
void setup() {
  frame = loadImage("frame.png");
  red = loadImage("red.png");
  orange = loadImage("orange.png");
  yellow = loadImage("yellow.png");
  green = loadImage("green.png");
  blue = loadImage("blue.png");
 // purple = loadImage("purple.png");
 // black = loadImage("black.png");
 // white = loadImage("white.png");
 // eraser = loadImage("eraser.png");
  brush = loadImage("brush.png");
  size(918, 790);
  background(#FAFEFF);
}
 
//-------------------------------------------------------------------------
 
 
void draw() {
  image(frame, 0, 0);
  image(red, 10, 735);
  image(orange, 60, 710);
  image(yellow, 110, 740);
  image(green, 160, 710);
  image(blue, 220, 738);
  //image(purple, 0, 0);
  //image(black, 0, 0);
  //image(grey, 0, 0);
  //image(white, 0, 0);
  //image(eraser, 0, 0);
  stroke(#E02A24);
  cursor(brush);
  if (mousePressed == true) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
 
void mousePressed() {
  if (mouseX == 10 && mouseY == 735){
    stroke(#D6120B);
  }
  if (mouseX == 60 && mouseY == 710){
    stroke(#FF9A2E);
  }
  if (mouseX == 110 && mouseY == 740){
    stroke(#FFEB2E);
  }
  if (mouseX == 160 && mouseY == 710){
    loop();
    stroke(#99EA21);
  }
}
 
 
void keyPressed() {
  save("my_drawing.png");
}

red

You’re only using very specific coordinates. Your mouse has to be in those exact coordinates for it to work.

1 Like

what coordinates should I use instead? I tried doing something like this;

if(mouseX >=5 && <=10 && mouseY >=725 && <=740){
stroke(#FF9A2E);
}

but it didn’t work, I’m not sure how to format it as I don’t want to use a int and am not sure how.

Try this:

if(mouseX>=5&&mouseX<=10&&mouseY>=725&&mouseX<=740){
stroke(#FF9A2E);
}

it still doesn’t work… do I need to add a loop in order for it to stay that colour?

I also tried

  if ((mouseX <15) && (mouseX >5) && (mouseY >725) && (mouseY <745)){
    stroke(#D6120B);
  }

but that didn’t work either. This should be a simple program no? I thought I would get it done quickly!

Maybe don’t use buttons, just check if a key is pressed.

so use keyPressed instead? That’s much easier so maybe I’ll do that just incase. But they are images and not ellipses, could that have something to do with it? the cursor is also an image.

Yeah, just try that.

i also didnt work

void keyPressed() {
  if(keyPressed == true && keyCode == 82){
  stroke(#D6120B);
  }
  else if(keyPressed == false && keyCode == 82){
  stroke(#D6120B);
  }
  if(keyPressed == true && keyCode == 79){
  stroke(#FF9A2E);
  }
  else if(keyPressed == false && keyCode == 79){
  stroke(#FF9A2E);
  }
  if(keyPressed == true && keyCode == 89){
  stroke(#FFEB2E);
  }
  else if(keyPressed == false && keyCode == 89){
  stroke(#FFEB2E);
  }
  if(keyPressed == true && keyCode == 71){
  stroke(#99EA21);
  }
  else if(keyPressed == false && keyCode == 71){
  stroke(#99EA21);
  }
}

Hi, @alessandrajoy
You also have to store your color in a public variable.
The following may work, tell me if not !

color brushColor=color(0,0,0);

void mousePressed() {
  if (mouseX >= 5 && mouseX <= 15  && mouseY >= 730 && mouseY <= 740) brushColor=color(214,18,11);
  if (mouseX >= 55 && mouseX <= 65  && mouseY >= 705 && mouseY <= 715) brushColor=color(255,154,46);
  if (mouseX >= 105 && mouseX <= 115  && mouseY >= 730 && mouseY <= 740) brushColor=color(255,235,46);
  if (mouseX >= 160 && mouseX <= 170  && mouseY >= 705 && mouseY <= 715) brushColor=color(153,234,33);
  stroke(brushColor);
}

1 Like

You could also use this kind of trick :

color brushColor=color(0,0,0);

void mousePressed() {
  if (mouseY >= 705) brushColor=get(mouseX,mouseY);
  stroke(brushColor);
}
1 Like

Please don’t do it like that… just do (I’m saying don’t do it like @alessandrajoy’s example.)
if(key=='(key here')){
stuff
}