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 › can't invoke drawing methods within a static class
Page Index Toggle Pages: 1
can't invoke drawing methods within a static class (Read 394 times)
can't invoke drawing methods within a static class
Apr 8th, 2006, 4:18pm
 
Hi!

I made a static class, but apparently it isn't possible to invoke the drawing methods like line from a static class.

Then I tried to make a normal class that handles the drawing, but I can't construct this class in the static class.

What's the reason for this behavior?


Quote:


void setup() {
 size(400, 400);
 background(255,255,255);
 noLoop();
}

void draw() {

}

static class Vec {
 
 VecViewer vw; // not possible?
 
 Vec() {
   line(10,10,50,50); // why can't I invoke drawing methods within a static class?
 }
 
}

class VecViewer {

 VecViewer() {
   
 }

}




controller
Re: can't invoke drawing methods within a static c
Reply #1 - Apr 8th, 2006, 4:27pm
 
you don't need to make the class 'static'. read up on the java spec for how static classes work, it won't have access to its parent class the way that you'd like because it doesn't deal with instances the same way.
Re: can't invoke drawing methods within a static c
Reply #2 - Apr 8th, 2006, 5:47pm
 
thanks for the answer. I'll check the java spec.

the reason I made the class static was because I had mixed up static and non-static methods that I deleted before posting the code here and I wanted to access the static methods in a static context.

The full class implementation looks like this:

Quote:


// vector class
static class Vec {
 
 float x,y;
 
 Vec() {
   vw = new VectorViewer();
   x = 0;
   y = 0;
 }
 
 Vec(float _x, float _y) {
   vw = new VectorViewer();
   x = _x;
   y = _y;
 }
 
 void display() {
   vw.display(x,y);
 }
 
 void normalize() {
   x /= length();
   y /= length();
 }
 
 float length() {
   return sqrt(x*x + y*y);
 }
 
 float lengthSquared() {
   return x*x + y*y;
 }
   
 // ** static methods ** //  
 // dot product
 static float dot(Vec a, Vec b) {
   return a.x * b.x + a.y * b.y;
 }  
 
 // multiplication
 static Vec mult(float factor, Vec a) {
   return new Vec(factor*a.x,factor*a.y);
 }
 
 // computes the projection of a onto b
 static Vec proj(Vec a, Vec b) {
   return mult(dot(a,b)/b.lengthSquared(),b);
 }
}

class VectorViewer {
 
 VectorViewer() {
   
 }
 
 void display(float x, float y) {
   line(0,0,x,y);
 }
}

Re: can't invoke drawing methods within a static c
Reply #3 - Apr 8th, 2006, 6:10pm
 
Slightly out of the thread, but I see a bug in that code Smiley
Code:
void normalize() {
x /= length();
y /= length();
}


That isn't going to do what you want, you need to calculate length() once, then use that value for the division. The way you have it at the moment, length() changes answer between the two calculations.
Re: can't invoke drawing methods within a static c
Reply #4 - Apr 8th, 2006, 6:35pm
 
yeah right, thanks for the advise.
Re: can't invoke drawing methods within a static c
Reply #5 - Apr 10th, 2006, 1:51pm
 
yup, you also need to do this:

Code:
void normalize() {
float len = length();
if (len != 0) {
x /= len;
y /= len;
}
}

otherwise you'll get numbers that show up as NaN or Infinity when the length is zero.
Page Index Toggle Pages: 1