Mouse moved() function

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.

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.

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://www.processing.org/reference/color_.html
https://processing.org/examples/ Check out the Input examples

Enough to get you started:


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
}

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)

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() 
{

}

And also the GUI examples.

i just don’t get it…

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.

:)

mouse functions, maybe?

1 Like

Looks promising…

:)