Using multiple mouse functions

this is what i’m trying to do:

  • Create a program that will change the background color based on the mouse.
  • You will create a setup(), draw() function. The size of the frame will by 400 by 400.
  • Create a mousePressed() function . Check to see if the RIGHT or LEFT mouse button has been pressed. Change the background color to red if the LEFT mouse is pressed and blue if the right mouse is pressed.
  • Create a mouseReleased() function. Change the background to yellow.
  • Create a mouseMoved() function. Change the background to green.
  • Create a mouseDragged() function . Change the background to white.

**Can you check my code to see if it works right? **

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

color c1=color(255, 0, 0);//red, left
color c2=color(0, 0, 255);//blue, right
color c3=color(255, 255, 0);//yellow
color c4=color(0, 255, 0);//green
color c5=color(255, 255, 255);//white

void draw()
{
mousePressed();
    background (0);

  println("mouse was pressed", width, height);
  if(mouseButton ==LEFT)
  {
     background(c1);
    println("and it was the left button", width, height);
  }
  println("mouse was pressed", width, height);
  if(mouseButton ==RIGHT)
  {
    background(c2);
    println("and it was the right button", width, height);
  }
}

void mouseReleased()
{
    background(c3);
}

void mouseMoved()
 {
   background(c4);
}

void mouseDragged()
{
  background(c5);
}

I don’t understand. Can’t you test it??

you forgot to do this.

See reference mousePressed(): https://processing.org/reference/mousePressed_.html

put the if-clauses from draw into mousePressed()

Remark

See this is very flickering. This is because you use multiple background()

use only ONE background at start of draw(). No other background allowed.

at start of draw() say background (bgcol);

instead of using background everywhere say bgcol=c1; or bgcol=c2; etc.

1 Like

I can get all the colors to show…just not the yellow for very long…is there a way to increase the amount of time that the yellow stays on the screen? the yellow is supposed to show when using mouse released

mouseReleased() is only active for a single frame. If your sketch is running at 60FPS, that’s just 1/60th of a second.

One way you could get the background to appear along is to decrease the frame rate to something lower - if, say, you set the max framerate to 5 FPS, you’ll have a whole 5th of a second to see it before the next frame covers it up.

You’re in luck; you can set the maximum frame rate (aka how often is draw() called every second) with the frameRate() function.

void setup() {
    size(400, 400);
    frameRate(5);
}

Thanks…that was super simple! I knew I need to use frameRate…i just wasn’t sure where to put it!

1 Like

Can you please post your entire Sketch?