We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Regional Mouse Click and Resulting Color Changes
Page Index Toggle Pages: 1
Regional Mouse Click and Resulting Color Changes (Read 1355 times)
Regional Mouse Click and Resulting Color Changes
Feb 9th, 2010, 2:26pm
 
Hello, everyone! I've been given an assignment, and I've almost got it, but not quite. Here's the gist of it:

Write a program so that a mouse click within a shape changes said shape. A mouse click outside of any shape should also alter the image somehow.

I've gotten my shapes to change colors, but I'm having an issue getting my image to change when I click outside my shapes. Specifically, I'd like all three of my shapes to be filled with white, and then revert to their original color when I click again. Here's the code:

//Variables
boolean button1=false;
boolean button2=false;
boolean button3=false;
boolean button4=false;

//Rectangle variables
float rX=50;
float rY=50;
float rW=100;
float rH=100;

//Circle variables
float cX=245;
float cY=45;
float cW=110;
float cH=110;

//Triangle variables
float tA=125;
float tB=200;
float tC=150;
float tD=125;

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

void draw(){
 //Draw initial sketch
 background(180);
 stroke(0);
 strokeWeight(3);
 //Rect color change
 if(button1){
   fill(9,106,16);
 }
 else if (button4){
   fill(255);
 }
 else{
   fill(255,13,0);
 }
 rectMode(CENTER);
 rect(100,100,100,100);
 //circle color change
 if(button2){
   fill(10,48,193);
 }
 else if (button4){
   fill(255);
 }
 else{
   fill(9,106,16);
 }
 ellipse(300,100,110,110);
 //Triangle color change
 if(button3){
   fill(255,13,0);
 }
 else if (button4){
   fill(255);
 }
 else{
   fill(10,48,193);
 }
 triangle(200,200,275,325,125,325);
}
 
 //Rectangle
 void mousePressed(){
   //Change rect color on/off when mouse click inside rect
   if(mouseX > rX && mouseX < rX+rW && mouseY > rY && mouseY < rY+rH){
   button1=!button1;
 }
 //Change circle color on/off when mouse click inside circle
 if(mouseX> cX && mouseX < cX+cW && mouseY > cY && mouseY < cY+cH){
   button2=!button2;
 }
 //Change ellipse color on/off when mouse click inside triangle
 if(mouseX> tA && mouseX < tA+tC && mouseY > tB && mouseY < tB+tD){
   button3=!button3;
 }
 //Change all color when mouse click not inside shapes
 if(mouseX<rX && mouseX>rX+rW && mouseX<cX && mouseX >cX+cW && mouseX<tA && mouseX>tA+tC && mouseY<rY && mouseY>rY+rH && mouseY<cY && mouseY>cY+cH && mouseY<tB && mouseY>tB+tD){
   button4=!button4;
 }
 }

Thanks!  Smiley
 
P.S. I'm a little new to Processing, so please be as specific as possible. Simple explanations are also appreciated, since we've only covered basic commands so far (as in, we've only just scratched loops).
Re: Regional Mouse Click and Resulting Color Changes
Reply #1 - Feb 9th, 2010, 5:03pm
 
Quote:
void mousePressed(){ // <-- void is the return type, which means this function doesn;t return a value.
  //Change rect color on/off when mouse click inside rect
  if(mouseX > rX && mouseX < rX+rW && mouseY > rY && mouseY < rY+rH){
    button1=!button1;
    return; // Clicked on the rectangle, so exit mousePressed() at this point, returning no value.
  }
  //Change circle color on/off when mouse click inside circle
  if(mouseX> cX && mouseX < cX+cW && mouseY > cY && mouseY < cY+cH){
    button2=!button2;
    return; // Clicked on the Circle, so exit mousePressed() at this point, returning no value.
}
  //Change ellipse color on/off when mouse click inside triangle
  if(mouseX> tA && mouseX < tA+tC && mouseY > tB && mouseY < tB+tD){
    button3=!button3;
    return; // Clicked on the triangle, so exit mousePressed() at this point, returning no value.
  }
  // We can only get here if none of the other buttons were clicked, since they all exit from the
  // function mousePressed() by using the keyword "return", which exits the function it's in at that point
  // (and would return usually a value in a non-void function, but since this is a void function, they don't).
  //Change all color when mouse click not inside shapes
    button4=!button4;
}



Of course, now that that's working, we see that there are some more problems relating to when the shapes turn white... (for example, click the circle and then the background).

And welcome! You are doing great!  Grin
Re: Regional Mouse Click and Resulting Color Changes
Reply #2 - Feb 9th, 2010, 5:11pm
 
Thank you! This helped so much.  Grin
Re: Regional Mouse Click and Resulting Color Changes
Reply #3 - Feb 9th, 2010, 5:16pm
 
Also, you can click close to some of the shapes (but not on them), and they will still change color. This is because you are checking if the mouse is within the rectangle the shape would be inscribed in, and not if the mouse is EXACTLY inside the shape.

For the circle, I suggest you look into the dist() function. If the mouse's coordinates are close enough to the circle's center, you know the mouse is in the circle.

As for the triangle, what value must mouseY be at a given mouseX to still be in the triangle? You'll really have to think about it...

Maybe you could write three separate function to check if the mouse is over a shape:
"boolean isMouseOverRect(int input_mouseX, int input_mouseY)"
"boolean isMouseOverCircle(int input_mouseX, int input_mouseY)"
"boolean isMouseOverTri(int input_mouseX, int input_mouseY)"
each of which could return true or false depending on if the mouse is within the shape.
Page Index Toggle Pages: 1