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.
Page Index Toggle Pages: 1
static context? (Read 464 times)
static context?
Jun 26th, 2007, 8:59pm
 
Hello I'm new and am hung up on why this won't work


rectPos rect01;


void setup(){
 size(200,200);
 background(150);
rect01 = new rectPos(width/2, height/2, 50, 60);
}

void draw(){
 
 rect01.show();
 
 if(rect01.count<3){
 rect01.update();
 }
}
 
 class rectPos{
   int x;
   int y;
   int sizeX;
   int sizeY;
   int count=1;
   
   rectPos(int _x, int _y, int _sizeX, int _sizeY){
     x=_x;
     y=_y;
     sizeX=_sizeX;
     sizeY=_sizeY;
     
   }
   
   void update(){
     x=x/2;
     y=y/2;
     sizeX=sizeX/2;
     sizeY=sizeY/2;
     count++;
   }
   
   void show(){
     rectDraw.display(x,y,sizeX,sizeY);
   }
 }
 
 class rectDraw extends rectPos{
   int $x;
   int $y;
   int $sizeX;
   int $sizeY;
   
   display(float _x,float _y,float _sizeX,float sizeY){
     $x=_x;
     $y=_y;
     $sizeX=_sizeX;
     $sizeY=_sizeY;
     rect($x,$y,$sizeX,$sizeY);
   }
 }

The error message reads "The method "display" is not static, and cannot be accessed in this static context."
I realize there are a number of ways around this, but am wondering why this wouldn't work and what it means for a method to be static.

thanks for the time
Re: static context?
Reply #1 - Jun 26th, 2007, 9:57pm
 
Calling the extended class from the class that's being extended...

Quick and dirty fix.

Quote:

rectPos rect01;

void setup(){
 size(200,200);
 background(150);
 rect01 = new rectPos(width/2, height/2, 50, 60);
}

void draw(){
 rect01.show();
 if(rect01.count<3){
   rect01.update();
 }
}

class rectPos{
 int x;
 int y;
 int sizeX;
 int sizeY;
 int count=1;
 rectDraw rd;

 rectPos(int _x, int _y, int _sizeX, int _sizeY) {
   x=_x;
   y=_y;
   sizeX=_sizeX;
   sizeY=_sizeY;
   rd = new rectDraw();
 }

 void update(){
   x=x/2;
   y=y/2;
   sizeX=sizeX/2;
   sizeY=sizeY/2;
   count++;
 }

 void show(){
   rd.display(x,y,sizeX,sizeY);
 }  
}  

class rectDraw {
 float x;
 float y;
 float sizeX;
 float sizeY;

 rectDraw() {
 }

 void display(float _x, float _y, float _sizeX, float _sizeY) {
   x=_x;
   y=_y;
   sizeX =_sizeX;
   sizeY =_sizeY;
   rect(x,y,sizeX,sizeY);
 }
}


Google "inheritance" and "polymorphism" for more info how to extend classes.
Or read How my Dog learned Polymorphism


Simpler: place the display function inside the rectPos class. Unless different classes are going to use the rectDraw class.

Quote:

class rectPos {  
 int x;  
 int y;  
 int sizeX;  
 int sizeY;  
 int count=1;  

 rectPos(int _x, int _y, int _sizeX, int _sizeY) {  
   x=_x;  
   y=_y;  
   sizeX=_sizeX;  
   sizeY=_sizeY;  
 }  

 void update(){  
   x=x/2;  
   y=y/2;  
   sizeX=sizeX/2;  
   sizeY=sizeY/2;  
   count++;
 }  
 
 void display() {
   rect(x, y, sizeX, sizeY);
 }
}
Re: static context?
Reply #2 - Jun 26th, 2007, 11:17pm
 
thanks, that was super quick
Page Index Toggle Pages: 1