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
Learning OOP (Read 1942 times)
Learning OOP
May 26th, 2010, 6:04pm
 
Hi. I'm trying to understand OOP in Processing. I wrote piece of code and I don't know what is wrong.

Code:
class ControlPoint {

int x, y;
color colour;
int radius;

void ControlPoint() {
x = width / 2;
y = height / 2;
colour = #FFCC00;
radius = 5;
}

void display() {
stroke(colour);
fill(colour);
ellipse(x, y, radius, radius);
}

}

ControlPoint cp;

void setup() {
size(50, 50);
cp = new ControlPoint();
}

void draw() {
cp.display();
}
Re: Learning OOP
Reply #1 - May 26th, 2010, 9:06pm
 
Do not define a return type for a constructor, that is where your problem is. Remove the 'void' type so it becomes what you thought it was.

Code:
void ControlPoint() {
   x = width / 2;
   y = height / 2;
   colour = #FFCC00;
   radius = 5;
 }



becomes:

Code:
ControlPoint() {
   x = width / 2;
   y = height / 2;
   colour = #FFCC00;
   radius = 5;
 }
Re: Learning OOP
Reply #2 - May 27th, 2010, 4:49am
 
Thanks. Do you know where may I find some more advanced tutorials about OOP in Processing? With descructors, exception handling etc. I read this one:

http://processing.org/learning/objects/

but it's good just for start.
Re: Learning OOP
Reply #3 - May 27th, 2010, 5:53am
 
Java has no destructors, garbage collection is supposed to take care of that, and you can use the finally keyword too.
Exception handling isn't really about OOP...

Actually, any tutorial about OOP in Java (and there are plenty of them!) is good enough for Processing too...
Re: Learning OOP
Reply #4 - May 27th, 2010, 7:27am
 
If you're serious about learning OOP, you might consider moving from the PDE to Eclipse.  This page is a pretty good explanation of how to get started using Processing + Eclipse.

One big advantage of Eclipse is that the error messages are more informative and usually come a lot sooner (i.e., warnings in the editor window when you do something "strange"), which can be helpful when you're learning OOP.
Re: Learning OOP
Reply #5 - May 27th, 2010, 7:36am
 
Quote:
Java has no destructors

There is the finalize() method that is invoked by the JVM when the object is garbage collected but it is rare that you might want to use it.

Quote:
If you're serious about learning OOP, you might consider moving from the PDE to Eclipse.

Eclipse can be quite daunting for newbies to programming which is why Processing is so good but I agree with Smitty that is an excellent IDE for Java programming and with the Proclipsing Eclipse plugin it is easy to translate existing Processing sketches to Eclipse for experimentation.
Re: Learning OOP
Reply #6 - May 27th, 2010, 7:54am
 
Quark wrote on May 27th, 2010, 7:36am:
There is the finalize() method that is invoked by the JVM when the object is garbage collected but it is rare that you might want to use it.

Yes. I didn't want to mention it to a "newbie" because every text on Java advises not to use it... Smiley I never had to, either.
Re: Learning OOP
Reply #7 - May 27th, 2010, 11:49am
 
The problem is, that I'm using Processing.js. "import" is still unimplemented, so I can't divide code into packages. I think, I could do it using php "include". But it's not as tidy as "import"Smiley.
Page Index Toggle Pages: 1