# Mouse moved() function

**URL:** https://discourse.processing.org/t/mouse-moved-function/24373
**Category:** Coding Questions
**Tags:** homework
**Created:** [October 5, 2020, 11:16pm UTC](https://discourse.processing.org/t/mouse-moved-function/24373 "2020-10-05T23:16:04Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [October 5, 2020, 11:16pm UTC](https://discourse.processing.org/t/mouse-moved-function/24373/1 "2020-10-05T23:16:04Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ac824c9e14550700c37ca286408b712c2d4b0233.png)

This is what I am trying to re-create.

I have this so far…i have the 1st square, but i don’t know how to get the last 3.

```auto
void setup()
{size (400, 400);
background (0);}

int value = 0;

void draw() {
  fill(255, 255, 0);
  rect(0, 0, 200, 200);

}
void mouseMoved()
{
  
}

```

Here are the directions:

Create a program that will create a square and fill it with a certain color based on the x and y location of the mouse. Use the mouseMoved () function and create appropriate if statements using the appropriate condition and boolean logic operator to create the square and fill it with a color. The sketch should contain a **setup (),****draw () **as well as the** mouseMoved() function**. The background will be set to black and will change to the appropriate color when the mouse enters the area specified by the condition. All other areas will remain black.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 6, 2020, 12:07am UTC](https://discourse.processing.org/t/mouse-moved-function/24373/2 "2020-10-06T00:07:14Z")

</div>

Hello,

This is a very achievable homework exercise.

I am assuming that mouse over a rectangle sets the color of that rectangle and background to black.

See these references:  
[https://processing.org/reference/rect\_.html](https://processing.org/reference/rect_.html)  
[https://www.processing.org/reference/color\_.html](https://www.processing.org/reference/color_.html)  
[https://processing.org/examples/](https://processing.org/examples/) Check out the Input examples

Enough to get you started:

```auto

color c1;
// Add more colors for each rectangle

void setup()
  {
  size (400, 400);
  background (0);
  }

void draw() 
  {
  fill(c1);
  rect(0, 0, 200, 200);
  // 3 more rectangles required
  }

void mouseMoved()
{
println(mouseX, mouseY); //Displays mouse location
// 4 conditional statements to check for location of mouseX and mouseY
// These conditions when true set the 4 colors for the 4 rectangles
}

```

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [October 6, 2020, 8:29pm UTC](https://discourse.processing.org/t/mouse-moved-function/24373/3 "2020-10-06T20:29:20Z")

</div>

This is what I have now. It draws the rectangles all at once. I’m trying to get it to move the rectangles when I move the mouse and only show one color at a time.

Any advice?  
(create appropriate if statements using the appropriate condition and boolean logic operator to create the square and fill it with a color)

```auto
void setup()
{size (400, 400);
background (0);}

color c1=color(255, 255, 0);
color c2=color(255, 0, 0);
color c3=color(0, 0, 255);
color c4=color(0, 255, 0);

int value = 0;

void draw() 
{
  fill(c1);
  rect(0, 0, 200, 200);

  fill(c2);
  rect(200, 0, 200, 200);

  fill(c3);
  rect(0, 200, 200, 200);

  fill(c4);
  rect(200, 200, 200, 200);
  
}
void mouseMoved() 
{

}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 6, 2020, 8:48pm UTC](https://discourse.processing.org/t/mouse-moved-function/24373/4 "2020-10-06T20:48:51Z")

</div>

> [@thughes](#):
>
> Any advice?  
> (create appropriate if statements using the appropriate condition and boolean logic operator to create the square and fill it with a color)

> [@glv](#):
>
> ```auto
> void mouseMoved()
> {
> println(mouseX, mouseY); //Displays mouse location
> // 4 conditional statements to check for location of mouseX and mouseY
> // These conditions when true set the 4 colors for the 4 rectangles
> }
> 
> ```

> [@glv](#):
>
> [Examples / Processing.org](https://processing.org/examples/) Check out the Input examples

And also the GUI examples.

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [October 7, 2020, 12:21am UTC](https://discourse.processing.org/t/mouse-moved-function/24373/5 "2020-10-07T00:21:31Z")

</div>

i just don’t get it…

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 7, 2020, 12:35am UTC](https://discourse.processing.org/t/mouse-moved-function/24373/6 "2020-10-07T00:35:10Z")

</div>

> [@thughes](#):
>
> just don’t get it…

> [@glv](#):
>
> [Examples / Processing.org](https://processing.org/examples/) Check out the Input examples

Which example (from above) is most relevant to your homework exercise?

There is a learning curve to programming and you have to go through this and hopefully enjoy it.

`:)`

---

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [October 7, 2020, 12:51am UTC](https://discourse.processing.org/t/mouse-moved-function/24373/7 "2020-10-07T00:51:37Z")

</div>

mouse functions, maybe?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 7, 2020, 10:46am UTC](https://discourse.processing.org/t/mouse-moved-function/24373/8 "2020-10-07T10:46:16Z")

</div>

Looks promising…

`:)`
