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 & HelpSyntax Questions › "Springs" Circles Color (Newbie)
Page Index Toggle Pages: 1
"Springs" Circles Color (Newbie) (Read 610 times)
"Springs" Circles Color (Newbie)
Sep 28th, 2006, 11:49am
 
I'm workin with the http://processing.org/learning/examples/springs.html code and
try to get circles with different color each  before to "over" them but can't get the way...

I found:

void draw()
 {
   if(over) {
     fill(153);
   } else {
     fill(255);
   }

but each change affect the 3 circles together,
can they have independent behaviour?
Thanks
Re: "Springs" Circles Color (Newbie)
Reply #1 - Sep 28th, 2006, 3:56pm
 
The example you are referring to is programmed in an "object-oriented" manner, meaning the code for displaying a Spring object lives inside the Spring "class."  

Code:

class Spring {
// all about a Spring
}


Since fill() uses a hard-coded value in the class, all Spring objects will always be colored with that same value, no matter how many Springs you make:

Code:

void draw()
{
if(over) {
fill(153);
} else {
fill(255);
}
ellipse(tempxpos, tempypos, size, size);
}


If you want to make Springs each with a different color, you need to introduce a color variable inside the class.

Code:

class Spring
{
color normColor;
color overColor;


// Constructor
Spring(float x, float y, int s, float d, float m,
float k_in, Spring[] others, int id,
color c1, color c2)
{
normColor = c1;
overColor = c2;
}

void draw()
{
if(over) {
fill(overColor);
} else {
fill(normColor);
}
ellipse(tempxpos, tempypos, size, size);
}
}


Then you can make two Spring objects with different colors:

Code:

springs[0] = new Spring( 70, 160, 20, 0.98, 8.0, 0.1, springs, 0, color(255), color(150));
springs[1] = new Spring(150, 110, 60, 0.95, 9.0, 0.1, springs, 1), color(255,0,0), color(0,0,255));


For more about how objects and classes work in Processing, check out:

http://itp.nyu.edu/icm/shiffman/week3/index.html
Re: "Springs" Circles Color (Newbie)
Reply #2 - Sep 29th, 2006, 7:43am
 
Thanks for the explanation , sounds clear (quite hard for me, no coder..)will try again and again, I'll tell you
Re: "Springs" Circles Color (Newbie)
Reply #3 - Oct 5th, 2006, 12:52pm
 
works!
Page Index Toggle Pages: 1