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
OOP Woes (Read 1244 times)
OOP Woes
Jun 9th, 2005, 12:57am
 
I am starting to experiment with object-oriented programming within the Processing environment and have made some headway, albeit with much stumbling. At this point, however, I am stuck on this particular problem:

I want to initialize a class with a number of parameters that apply to all members of the class, but not to any particular instance. From what I have read, I understand that this can be done with a static method within the class definition, and this method can be invoked something like this: Class.init(params);

Here's the source code of a test example:

Circle myCircle;
float ranColor;

void setup(){
 size(300,300);
 background(255);
 framerate(10);
 fill(0);
 noStroke();
 ellipseMode(CENTER);
 myCircle = new Circle();
 Circle.init(width/2, height/2, width/2);
}
 
void draw(){
 ranColor=random(100);
 myCircle.update(ranColor);
}

public class Circle{

 float xPos;
 float yPos;
 float radius;
 float col;

 public static void init(float x, float y, float r){
   xPos   = x;
   yPos   = y;
   radius = r;
 }
 
 Circle(){
 }
 
 public void update(float c){
   col = c;
   fill(col);
   ellipse(xPos, yPos, radius, radius);
   redraw();
 }
}

The problem is, however, that when I run the sketch, I get the following error:

...Semantic Error: The static method "init" is invalid, because it is enclosed in an inner class, "Circle"...

I've tried for a few days to figure this out, but have been unsuccessful. Could someone please show me the way?

Thanks,

~BB~
Re: OOP Woes
Reply #1 - Jun 9th, 2005, 2:50am
 
unfortunatly, in processing you can not put static stuff in a class, since what it does is it has one class that runs all your stuff, and java doesn't let classes in classes have static things.

now if you put it outside any class it would work, but if you want it in a class, well, fry, if your reading this you can probaly help better
Re: OOP Woes
Reply #2 - Jun 9th, 2005, 7:03am
 
As I read your original problem, I see this as a solution:
Code:
int var1 = 200;
String name = "John";
Hello goodMorning;
Hello goodBye;

void setup() {
goodMorning = new Hello("Good morning");
println(goodMorning.sayHello());
goodBye = new Hello("Good bye");
println(goodBye.sayHello());
}

void draw() {

}

class Hello {
String internalName = name;
String internalMessage;
int size = var1;

Hello(String message) {
internalMessage = message;
}

String sayHello() {
return internalMessage + " " + internalName + "!";
}
}

When you run this example (P0090) you get the result: Quote:
Good morning John!
Good bye John!

I set up a variable called String name = "John"; outside the class definition, and then used it inside the class definition, so that all objects of Hello would have the name John - I think this acheives your original goal. Both Hello objects goodMorning and goodBye both reference to "John".
Re: OOP Woes
Reply #3 - Jun 17th, 2005, 6:38pm
 
Thanks! That was quite a bit easier than I imagined...
Re: OOP Woes
Reply #4 - Jul 1st, 2005, 9:02pm
 
So classes in Processing apps cannot have static methods? I'm guessing there was some support for this at one time since Dan Shiffman's Vector3D class on his Nature of Code website has a bunch of static methods. What happened?
Re: OOP Woes
Reply #5 - Jul 2nd, 2005, 3:58am
 
I believe Dan Shiffman's Vector3D class, is an external java class, not an internal Processing class. So it is a separate file with a .java extension and can use static methods
Re: OOP Woes
Reply #6 - Aug 31st, 2005, 8:59pm
 
I just got back to this after a processing hiatus this summer and this was indeed correct. I just changed Vector3D.pde to Vector3D.java and everything compiled nicely. Thanks!
Page Index Toggle Pages: 1