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 › newbie needs help with classes
Pages: 1 2 
newbie needs help with classes (Read 1607 times)
newbie needs help with classes
Jan 17th, 2010, 11:46am
 
hello,

three days ago i had my first contact with processing.
and well -  i like it. Smiley

at the moment i try to change sketches i found in the web.
and until yet this was no bad idea - i've learned a lot.

but with this project i need help.
i try to change following code into classes on two tabs
and make it as simple as possible
(get rid of everything i don't need to get the same result)
it's a very simple program...
three lines move with different speed on the y-axis
and two of them changes color from red to yellow/cyan
the slowest is always yellow.


beyond this i'm asking myself
on how many ways you can write this
by getting the same result?
what is the simplest way to do it?

this is the state of play

Code:


// Declare and construct tree objects (h1, h2, h3) from the class HLine
// Objects habe different kind of color behavior.
HLine h1 = new HLine(color(255), color(0), color(0), -100, 2.0);
HLine h2 = new HLine(color(255), color(0), color(0), -30, 2.5);
HLine h3 = new HLine(color(0), color(255), color(0), -30, 0.4);

void setup()
{
size(255, 255);
frameRate(24);
smooth();
}

void draw() {
background(170);
h1.update();
h2.update();
h3.update();
h1.tempColor01();
h2.tempColor02();
h3.tempColor03();
}

class HLine {
color r, g, b;
float ypos, speed;

HLine (color tempR, color tempG, color tempB, float y, float s) {
r = tempR;
g = tempG;
b = tempB;
ypos = y;
speed = s;

}
void update() {
ypos += speed;
if (ypos > width) {
ypos = 0;
}

line(0, ypos, width, ypos);
}

void tempColor01() {

float g = ypos;
if (g > height) {
g = (0);
}
stroke(255,g,b);
strokeWeight(4);
line(0, ypos, width, ypos);

}

void tempColor02() {

float b = ypos;
if (b > height) {
b = (0);
}
stroke(255-b,b,b);
strokeWeight(2);
line(0, ypos, width, ypos);

}
void tempColor03() {

stroke(255,255,0);
strokeWeight(0);
line(0, ypos, width, ypos);

}

}




and it looks like this with two tabs... but it does not work yet.  Sad

Code:


HLine myh1;
HLine myh2;
HLine myh3;




void setup(){

size(255, 255);
frameRate(24);
smooth();

}

void draw() {
background(170);
myh1.update();
myh1.display();
myh2.update();
myh2.display();
myh3.update();
myh3.display();

float c = color();

myh1 = new HLine(color(255, c, c), -100, 2.0);
myh2 = new HLine(color(255-c, c, c), -30, 2.5);
myh3 = new HLine(color(255, 255, 0), -30, 0.4);


}




and the 2nd tab named HLine"
Code:

class HLine{

color c;
float ypos, speed;


HLine(color tempC, float y, float s) {

c = tempC = color();
ypos = y;
speed = s;
}


void display() {
fill (c);
line(0, ypos, width, ypos);
stroke (s);
}


void update() {
ypos += speed;
if (ypos > width) {
ypos = 0;

}
}

}



Re: newbie needs help with classes
Reply #1 - Jan 17th, 2010, 11:58am
 


try close processing and restart processing - it's always good to do this after changing your tabs.

Greetings!
Re: newbie needs help with classes
Reply #2 - Jan 17th, 2010, 12:50pm
 
Chrisir wrote on Jan 17th, 2010, 11:58am:
try close processing and restart processing - it's always good to do this after changing your tabs.

Greetings!



@Chrisir
that doesn't work... but thank you for this hint.
Re: newbie needs help with classes
Reply #3 - Jan 17th, 2010, 12:56pm
 
I think you have to do the

 myh1 = new HLine(color(255, c, c), -100, 2.0);    
 myh2 = new HLine(color(255-c, c, c), -30, 2.5);  
 myh3 = new HLine(color(255, 255, 0), -30, 0.4);

stuff in setup not in draw.

draw gets repeated automatically all the time...

Greetings...


Re: newbie needs help with classes
Reply #4 - Jan 17th, 2010, 1:07pm
 
mmh, better.

but the lines are still black...
is it possible to work with "int c" when "c" is a variable defined in the class for the color of the lines?


Code:



HLine myh1;
HLine myh2;
HLine myh3;




int c;


void setup(){

size(255, 255);
frameRate(24);
smooth();


myh1 = new HLine(color(255, c, c), -100, 2.0);
myh2 = new HLine(color(255-c, c, c), -30, 2.5);
myh3 = new HLine(color(255, 255, 0), -30, 0.4);


}


void draw() {
background(170);
myh1.update();
myh1.display();
myh2.update();
myh2.display();
myh3.update();
myh3.display();


}
Re: newbie needs help with classes
Reply #5 - Jan 17th, 2010, 1:16pm
 


I wonder:
you didn't bring this into your new version:

void tempColor01() {

   float g = ypos;
   if (g > height) {
     g = (0);
   }
   stroke(255,g,b);
   strokeWeight(4);
   line(0, ypos, width, ypos);

 }

 void tempColor02() {

   float b = ypos;
   if (b > height) {
     b = (0);
   }
   stroke(255-b,b,b);
   strokeWeight(2);
   line(0, ypos, width, ypos);

 }
 void tempColor03() {

   stroke(255,255,0);
   strokeWeight(0);
   line(0, ypos, width, ypos);

 }

plus..........................

 h1.tempColor01();  
h2.tempColor02();  
h3.tempColor03();


in draw...

Greetings.



Re: newbie needs help with classes
Reply #6 - Jan 17th, 2010, 1:29pm
 
hey Chrisir,


it was the attempt to make the class more easy simple

and to work with the variable "c" in the color definition part of


Code:
myh1 = new HLine(color(255, c, c), -100, 2.0);    
myh2 = new HLine(color(255-c, c, c), -30, 2.5);  
myh3 = new HLine(color(255, 255, 0), -30, 0.4);




cheers
Re: newbie needs help with classes
Reply #7 - Jan 17th, 2010, 1:36pm
 

well...

there are a few things to discuss...

eg.
give c a value first
eg.
c=20;

then

instead of   void display() {
   fill (c);
   stroke (s);
   line(0, ypos, width, ypos);
 }

that's better

(beside that you use s(peed) as a color)

maybe you want

 void display() {
   fill (c);
   stroke (c);
   line(0, ypos, width, ypos);
 }




Re: newbie needs help with classes
Reply #8 - Jan 17th, 2010, 1:59pm
 
thank u chrisir,

you are absolutely right with the s(peed)
what i realy wanted was a strokeWeight

now this code here works much better than the one before
the lines are colored
but the color is still not changin yet...

tab_01:

Code:
HLine myh1; 
HLine myh2;
HLine myh3;

int c;


void setup(){

 size(255, 255);
 frameRate(25);
 smooth();
 
 myh1 = new HLine(color(255, c, c), -100, 2.0, 4);
 myh2 = new HLine(color(255-c, c, c), -30, 2.5, 2);
 myh3 = new HLine(color(255, 255, 0), -30, 0.4, 0);

 
}


void draw() {
 background(170);

 myh1.update();
 myh1.display();
 myh2.update();  
 myh2.display();
 myh3.update();  
 myh3.display();


}



tab_02

Code:


class HLine{

 color c;
 float ypos, speed;
 float s;
 float sw;



 HLine(color tempC, float y, float s, float sweight) {  

   c = tempC;
   ypos = y;
   speed = s;
   sw = sweight;
 }

 void update() {
   ypos += speed;
   if (ypos > height) {
ypos = 0;
   }
   stroke (c);
   strokeWeight (sw);
   line(0, ypos, height, ypos);
 }


 void display() {
   c = color (c);
stroke (c);
   strokeWeight (sw);
   line(0, ypos, height, ypos);
 }




}
Re: newbie needs help with classes
Reply #9 - Jan 17th, 2010, 2:10pm
 


each of your three lines is an object of class HLine.
one of the properties is c.

instead of

void draw() {
 background(170);

 myh1.update();
 myh1.display();
 myh2.update();  
 myh2.display();
 myh3.update();  
 myh3.display();


}

you need eg.

void draw() {
 background(170);

 myh1.c =   myh1.c + 20;
 myh1.update();
 myh1.display();
 myh2.c = random (254);
 myh2.update();  
 myh2.display();
 myh3.c= 200;
 myh3.update();  
 myh3.display();


}



Re: newbie needs help with classes
Reply #10 - Jan 17th, 2010, 2:30pm
 
sorry, that doesn't work...

basically this should look like the code
i've posted first but with the function "c" in this part of the code
Code:

myh1 = new HLine(color(255, c, c), -100, 2.0);    
myh2 = new HLine(color(255-c, c, c), -30, 2.5);  
myh3 = new HLine(color(255, 255, 0), -30, 0.4);


to change the color of the lines according to there y position
Re: newbie needs help with classes
Reply #11 - Jan 17th, 2010, 2:40pm
 

Well, it was an example to show you how to change the color.

To have the color like the ypos you need to have something like your old tempColor01:

 void tempColor01() {

   float g = ypos;
   if (g > height) {
     g = (0);
   }
   stroke(255,g,b);
   strokeWeight(4);
   line(0, ypos, width, ypos);

 }

you can do it in draw threee times

   float g = myh1.ypos;
   if (g > height) {
     g = (0);
   }
   myh1.c=g;

etc.

Greetings !



Re: newbie needs help with classes
Reply #12 - Jan 18th, 2010, 5:16am
 
hey chrisir,

i've tried nearly everything now
and i don't know what to do anymore... Cheesy


i haven't figured out yet how to get a variable argument "ypos"
inside of the constructor method.


Code:
  myh1 = new HLine(color(255,  ypos, ypos), -100, 2.0, 0); 
myh2 = new HLine(color(255-ypos, ypos, ypos), -30, 2.5, 2);
myh3 = new HLine(color(0, 255, 0), -30, 0.4, 5);



this is the actual state of the code.

tab_01
Code:
HLine myh1; 
HLine myh2;
HLine myh3;

float ypos, speed;


void setup(){

size(255, 255);
frameRate(25);
smooth();
colorMode(RGB);


myh1 = new HLine(color(255, ypos, ypos), -100, 2.0, 0);
myh2 = new HLine(color(255-ypos, ypos, ypos), -30, 2.5, 2);
myh3 = new HLine(color(0, 255, 0), -30, 0.4, 5);


}


void draw() {
background(170);


myh1.update();
myh1.display();
myh2.update();
myh2.display();
myh3.update();
myh3.display();

}




and tab_02
Code:
class HLine{ 

color c;
float ypos, speed;
float sw;
float r, g, b;



HLine(color tempC, float y, float s, float sweight) {

c = tempC;
ypos = y;
speed = s;
sw = sweight;
}



void update() {
ypos += speed;
if (ypos > height) {
ypos = 0;
}
stroke (c);
strokeWeight (sw);
line(0, ypos, height, ypos);

}

void display() {

stroke (c);
strokeWeight (sw);
line(0, ypos, height, ypos);
}



}







Re: newbie needs help with classes
Reply #13 - Jan 18th, 2010, 6:37am
 


tab_02: in update
before the line
 stroke (c);

put

c=ypos;
Re: newbie needs help with classes
Reply #14 - Jan 18th, 2010, 9:17am
 
hello once again,

Quote:
tab_02: in update
before the line
stroke (c);

put

c=ypos;

         

unfortunately this was not the solution

with c=pos;

i ger the error "cannot convert from float to int"

with float c = pos;

it doesn't work as well


but thank u allredy for your patience
this is a more challenging peace of code than i've had expected

cheers
Pages: 1 2