# Stroke won't change colour when clicked

**URL:** <https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793>\
**Category:** Coding Questions\
**Created:** [June 8, 2018, 2:55am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793 "2018-06-08T02:55:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![alessandrajoy](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alessandrajoy](https://discourse.processing.org/u/alessandrajoy)\
**Post date:** [June 8, 2018, 2:55am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/1 "2018-06-08T02:55:22Z")

</div>

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.

```auto
//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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/2ad08810757b3dc94670b0b5344eaaf54fb8c51b.png)

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 8, 2018, 3:08am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/2 "2018-06-08T03:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alessandrajoy](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alessandrajoy](https://discourse.processing.org/u/alessandrajoy)\
**Post date:** [June 8, 2018, 3:13am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/3 "2018-06-08T03:13:35Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 8, 2018, 3:46am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/4 "2018-06-08T03:46:27Z")

</div>

Try this:

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

---

<div class="post-metadata">

**Author:** ![alessandrajoy](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alessandrajoy](https://discourse.processing.org/u/alessandrajoy)\
**Post date:** [June 8, 2018, 3:51am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/5 "2018-06-08T03:51:01Z")

</div>

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

 ![04%20PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/fc14db424fd31a821728b027d6b9133a18ecba88.jpg)

I also tried

```auto
  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!

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 8, 2018, 3:55am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/6 "2018-06-08T03:55:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alessandrajoy](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alessandrajoy](https://discourse.processing.org/u/alessandrajoy)\
**Post date:** [June 8, 2018, 3:57am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/7 "2018-06-08T03:57:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 8, 2018, 4:19am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/8 "2018-06-08T04:19:02Z")

</div>

Yeah, just try that.

---

<div class="post-metadata">

**Author:** ![alessandrajoy](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alessandrajoy](https://discourse.processing.org/u/alessandrajoy)\
**Post date:** [June 8, 2018, 1:55pm UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/9 "2018-06-08T13:55:24Z")

</div>

i also didnt work

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colin/32/230_2.png) [@colin](https://discourse.processing.org/u/colin)\
**Post date:** [June 8, 2018, 5:03pm UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/10 "2018-06-08T17:03:14Z")

</div>

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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colin/32/230_2.png) [@colin](https://discourse.processing.org/u/colin)\
**Post date:** [June 8, 2018, 5:10pm UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/11 "2018-06-08T17:10:23Z")

</div>

You could also use this kind of trick :

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

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

```

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 16, 2018, 4:30am UTC](https://discourse.processing.org/t/stroke-wont-change-colour-when-clicked/793/12 "2018-06-16T04:30:43Z")

</div>

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`  
`}`
