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 › Newby not quite getting something...brain hurts
Page Index Toggle Pages: 1
Newby not quite getting something...brain hurts (Read 856 times)
Newby not quite getting something...brain hurts
Jan 14th, 2010, 6:59pm
 
I am working through Daniel Shiffmans great book "Learning Processing",  here is my code.  my problem is when I hold the mouse over a specific area for a long time, instead of staying black, it "resets" to white, until I move the mouse away - I am not sure why this happens.  Any thoughts on this would be much appreciated!

Code:
//pract_exc_5_6_enhanced
//using logical operators to color one of four quadrants based on mouse position

//these are variable that color the quadrants
int a = 255; //top right quad
int b = 255; //top left quad
int c = 255; //bottom left quad
int d = 255; //bottom right quad

void setup() {
 size(200,200);
 background(255);
}


void draw() {
 
 rectMode(CORNER);
 stroke(0);
 line(width/2,0,width/2,height); //vertical line down center of window
 line(0,height/2,width,height/2); //horizontal line across center of window

//quadrant a - when the mouse moves over this area, fade into black, when the mouse leaves, fade into white
 if(mouseX > width/2 && mouseY < height/2){  
   a = a - 1; //get darker
   fill(a);
   rect(width/2,0,width/2,height/2);
 }
 else{
   a = a + 1; //get lighter
   fill(a);
   rect(width/2,0,width/2,height/2);
 }


//quadrant b  - when the mouse moves over this area, fade into black, when the mouse leaves, fade into white
 if(mouseX < width/2 && mouseY < height/2){
   b = b - 1; //get darker
   fill(b);
   rect(0,0,width/2,height/2);
 }
 else{
   b = b + 1; //get lighter
   fill(b);
   rect(0,0,width/2,height/2);
 }

//quadrant c  - when the mouse moves over this area, fade into black, when the mouse leaves, fade into white
 if(mouseX < width/2 && mouseY > height/2){
   c = c - 1; //get darker
   fill(c);
   rect(0,height/2,width/2,height/2);
 }
 else{
   c = c + 1; //get lighter
   fill(c);
   rect(0,height/2,width/2,height/2);
 }


//quadrant d  - when the mouse moves over this area, fade into black, when the mouse leaves, fade into white
 if(mouseX > width/2 && mouseY > height/2){
   d = d - 1; //get darker
   fill(d);
   rect(width/2,height/2,width/2,height/2);
 }
 else{
   d = d + 1; //get lighter
   fill(d);
   rect(width/2,height/2,width/2,height/2);
 }
 
//constrain the darkenesss variables
 a = constrain(a,0,255);
 b = constrain(b,0,255);
 c = constrain(c,0,255);
 d = constrain(d,0,255);
 
}

Re: Newby not quite getting something...brain hurts
Reply #1 - Jan 14th, 2010, 9:23pm
 
Your constrain isn't working (because it gets processed too late) and the colours are going below zero and wrapping. If you add an if statement:

if (a>0) a= a - 1; //get lighter

It will fix this.
Re: Newby not quite getting something...brain hurts
Reply #2 - Jan 14th, 2010, 9:46pm
 
I Think it should replace the //get darker line instead
Re: Newby not quite getting something...brain hurts
Reply #3 - Jan 15th, 2010, 11:25am
 
Ah yes, I copied the wrong comment in there....
Page Index Toggle Pages: 1