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 › object oriented programming question
Page Index Toggle Pages: 1
object oriented programming question (Read 421 times)
object oriented programming question
Dec 1st, 2007, 4:59pm
 
Hi, I am new to object oriented programming, and the below code draws nothing to the screen ... what do I do wrong - do I activate my objects wrong???

import processing.opengl.*;

int amt, lnSections, cnters; // amount of objects activated

void setup(){
 size(700, 700, OPENGL);
 amt = 50;
 lnSections = 3;
 cnters = 50;
 // setup the line class
 for(int i=0; i< theLine.length; i++){
   theLine[i] = new Line(20, 20, 40, 40, 200, 200, 200, 200);
 } // end forloop

 // set up the line section class
 for(int i=0; i< lnSec.length; i++){
   lnSec[i] = new LnSec();
 } // end forloop
}

void draw(){
 background(0);
 stroke(200);
 // draw the lines
 for(int i=0; i< theLine.length; i++){
   theLine[i].drawLine();
 } // end forloop

 // draw the line sections
 for(int i=0; i< lnSec.length; i++){
   lnSec[i].drawLnSec();
 } // end forloop
} // end draw




Line [] theLine = new Line[amt];

class Line{
 int x, y, x1, y1, r, g, b, a;
 // each line can be addressed
 Line(int x, int y, int x1, int y1, int r, int g, int b, int a){
   this.x = x;
   this.y = y;
   this.y1 = y1;
   this.y1 = y1;
   this.r = r;
   this.g = g;
   this.b = b;
   this.a = a;
 }

// a simple draw line function, where each single line has its own parameters
void drawLine(){
   stroke(r, g, b, a);
   line(x, y, x1, y1);
 } // end draw-function

} //end Line class



LnSec [] lnSec = new LnSec[lnSections];

class LnSec{
 int tx, ty, tz;
 // each lineSection can now be addressed
 LnSec(){
 
 }
// here I activate the Line class, in order to draw the array
// of the line class in line sections ...
void drawLnSec(){
   pushMatrix();
   translate(20, 20, 0); // testposition
   //translate(tx, ty, tz);
   for(int i=0; i>theLine.length; i++){
    theLine[i].x = i*5;
    theLine[i].y = i*5;
    theLine[i].x1 = i*3;
    theLine[i].y1 = i*8;
    theLine[i].r = 200;
    theLine[i].g = 200;
    theLine[i].b = 200;
    theLine[i].a = 200;
   } // end forloop
   popMatrix();
 } // end draw-function

} //end LnSec class
Re: object oriented programming question
Reply #1 - Dec 1st, 2007, 9:32pm
 
You have to initialize your Line Array in the setup method:

Code:

Line [] theLine;
LnSec [] lnSec;
void setup(){
size(700, 700, OPENGL);
amt = 50;
lnSections = 3;
cnters = 50;
theLine = new Line[amt];
lnSec = new LnSec[lnSections];
// setup the line class
for(int i=0; i< theLine.length; i++){
theLine[i] = new Line(20, 20, 40, 40, 200, 200, 200, 200);
} // end forloop

// set up the line section class
for(int i=0; i< lnSec.length; i++){
lnSec[i] = new LnSec();
} // end forloop
}


Page Index Toggle Pages: 1