# Redraw function with color change/mouse interaction

**URL:** https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479
**Category:** Coding Questions
**Tags:** homework
**Created:** [January 6, 2023, 12:41am UTC](https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479 "2023-01-06T00:41:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lemoney](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lemoney](https://discourse.processing.org/u/lemoney)
#### Post date: [January 6, 2023, 12:41am UTC](https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479/1 "2023-01-06T00:41:51Z")

</div>

Hi, I’m in a class where we adapt code to fulfill the assignments. I’m working on mouse interaction with the redraw() function in Processing. What I’m trying to do is have the leaf colours change everytime the mouse is pressed. How do I get the leaf colour to change and where do I write this in? Eventually I would like the leaves to fall but I’m stuck. Any tips are appreciated. The class lesson involves functions and we haven’t gotten to classes yet…

```auto
/*
Task: Develop a program that incorporates more than one function. Once this has
been completed, try to vary or disrupt the pattern dynamically using mouse interaction.
*/

//VARIABLES
color c = color(189, 240, 201); //leaf color
color d = color(171, 211, 158);//leaf color after mouse pressed?

//SETUP
void setup()
{
  size(400,600);
  background(16, 106, 37);
  smooth();
  noStroke();
  noLoop();
}

//DRAW
void draw()
{
  fill(c);
  vine(40, 9, 16);
  vine(80, 9, 16);
  vine(120, 9, 16);
  vine(160, 9, 16);
  vine(200, 9, 16);
  vine(240, 9, 16);
  vine(280, 9, 16);
  vine(320, 9, 16);
  vine(360, 9, 16);
}

//New vine function
void vine(int x, int numLeaves, int leafSize)
{
  stroke(134, 100, 95);
  line(x, 0, x, height);
  noStroke();
  int gap = height / numLeaves;
  int direction = 1;
  for (int i = 0; i < numLeaves; i++)
  {
    int r = int(random(gap));
    leaf(x, gap*i + r, leafSize, direction);
    direction = -direction;
  }
}

//Previous leaf function 
void leaf(int x, int y, int size, int dir)
{
  pushMatrix();
  translate(x,y);
  scale(size);
  beginShape();
  vertex(1.0*dir, -0.7);
  bezierVertex(1.0*dir, -0.7, 0.4*dir, -1.0, 0.0, 0.0);
  bezierVertex(0.0, 0.0, 1.0*dir, 0.4, 1.0*dir, -0.7);
  endShape();
  popMatrix();
}
  void mousePressed()
  {
    redraw(); // Run the code in draw one time
    fill (d);//Leaf colour changes each time mouse is pressed?
}
type or paste code here

```

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [January 6, 2023, 6:36am UTC](https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479/2 "2023-01-06T06:36:44Z")

</div>

Hi @lemoney,

There are many ways to achieve the goal to get the color changed by mouse pressed, but it is important that you try to understand the process flow of your code.

First hint would be …  
check your draw function which is called on redraw.

> [@lemoney](#):
>
> ```auto
> void draw()
> {
> fill(c);
> 
> ```

No matter what you are doing beside, it is always set the fill color to `c`, even if you set it to `d` on other code places.  
If you want to change the color each time the mouse is pressed, you need to implement a kind of a switch for it.  
Maybe you can introduce a counter for the mouse pressed action and switch the color depending of its value. For odd counter values `fill(d)` and for even values `fill(c)`. If you only use two colors it could also be a boolean switch.

Give it a try and come back if you stuck…

Regarding…

> [@lemoney](#):
>
> I would like the leaves to fall

Let’s go step by step. First try the color thing…

Cheers  
— mnse

---

<div class="post-metadata">

### Author: ![lemoney](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lemoney](https://discourse.processing.org/u/lemoney)
#### Post date: [January 6, 2023, 2:47pm UTC](https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479/3 "2023-01-06T14:47:41Z")

</div>

Hello and thanks so much for the tips. I’ve managed to add colours using mouse interactions (left vs right click). What would be the best way to have the leaves fall after, say, the fifth mouse click (doesn’t matter if it’s a left or right)?? Here’s what I have so far…

```auto
//VARIABLES
color c = color(189, 240, 201); //leaf colour
color d = color(69, 131, 116);
color e = color(198, 103, 30);

//SETUP
void setup()
{
  size(400,600);
  background(16, 106, 37);
  smooth();
  noStroke();
  noLoop();
}

//DRAW
void draw()
{
  if (mousePressed && (mouseButton == LEFT))
  {
    fill (d);
    vine(40, 9, 16);
    vine(80, 9, 16);
    vine(120, 9, 16);
    vine(160, 9, 16);
    vine(200, 9, 16);
    vine(240, 9, 16);
    vine(280, 9, 16);
    vine(320, 9, 16);
    vine(360, 9, 16);
  }
  else if (mousePressed && (mouseButton == RIGHT))
  {
    fill (e);
    vine(40, 9, 16);
    vine(80, 9, 16);
    vine(120, 9, 16);
    vine(160, 9, 16);
    vine(200, 9, 16);
    vine(240, 9, 16);
    vine(280, 9, 16);
    vine(320, 9, 16);
    vine(360, 9, 16);
}
  else
  {
    fill (c);
    vine(40, 9, 16);
    vine(80, 9, 16);
    vine(120, 9, 16);
    vine(160, 9, 16);
    vine(200, 9, 16);
    vine(240, 9, 16);
    vine(280, 9, 16);
    vine(320, 9, 16);
    vine(360, 9, 16);

}
}

//New vine function
void vine(int x, int numLeaves, int leafSize)
{
  stroke(134, 100, 95);
  line(x, 0, x, height);
  noStroke();
  int gap = height / numLeaves;
  int direction = 1;
  for (int i = 0; i < numLeaves; i++)
  {
    int r = int(random(gap));
    leaf(x, gap*i + r, leafSize, direction);
    direction = -direction;
  }
}

//Previous leaf function 
void leaf(int x, int y, int size, int dir)
{
  pushMatrix();
  translate(x,y);
  scale(size);
  beginShape();
  vertex(1.0*dir, -0.7);
  bezierVertex(1.0*dir, -0.7, 0.4*dir, -1.0, 0.0, 0.0);
  bezierVertex(0.0, 0.0, 1.0*dir, 0.4, 1.0*dir, -0.7);
  endShape();
  popMatrix();
}
    void mousePressed()
  {
    redraw(); // Run the code in draw if mouse pressed
}

```

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [January 7, 2023, 10:43am UTC](https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479/4 "2023-01-07T10:43:23Z")

</div>

Hi @lemoney,

you can write it ie. as

```auto
void setup()
{
  size(400,600);
  background(16, 106, 37);
  smooth();
  noStroke();
  
  fill(c); // default for the first loop.

  noLoop();
}

void mousePressed()
{
  if (mouseButton == LEFT)       
    fill(d);
  else if (mouseButton == RIGHT) 
    fill(e);  
  else // middle mouse button
    fill(c);    
  redraw(); // Run the code in draw if mouse pressed
}

//DRAW
void draw()
{
  vine(40, 9, 16);
  vine(80, 9, 16);
  vine(120, 9, 16);
  vine(160, 9, 16);
  vine(200, 9, 16);
  vine(240, 9, 16);
  vine(280, 9, 16);
  vine(320, 9, 16);
  vine(360, 9, 16);
}

```

> [@lemoney](#):
>
> What would be the best way to have the leaves fall after, say, the fifth mouse click (doesn’t matter if it’s a left or right)??

This is imo out of scope based on the current concept of your program. You must imagine that ie. a falling leave is a kind of animation where you need to store and manipulation the leaves positions on each step. For this you need a different approach. For example something like …

- on setup
  - generate initial leaves [best way would be imo an array with leave objects]

- on each draw loop
  - clear screen
  - add new leaves [optional]
  - update the position of the leaves
  - draw/re-draw the leaves with new positions

Cheers  
— mnse

---

<div class="post-metadata">

### Author: ![lemoney](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lemoney](https://discourse.processing.org/u/lemoney)
#### Post date: [January 7, 2023, 9:59pm UTC](https://discourse.processing.org/t/redraw-function-with-color-change-mouse-interaction/40479/5 "2023-01-07T21:59:34Z")

</div>

Thank you sooo much. Yes, we’re moving onto arrays next week. Thanks again!
