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 › static methods in processing
Page Index Toggle Pages: 1
static methods in processing (Read 1265 times)
static methods in processing
Oct 13th, 2009, 11:38am
 
I came to Processing from Java, and I'm enjoying having all the graphical side of things set up and easy to use. One thing I used to do in Java, if I had a whole load of methods dealing with one particular issue (e.g colour) is to put them all in one class and then make them static. For my colourMethods class I would then call colourMethods.complementary(myColour), for example,  to get the complementary of the colour. It's exactly like calling Math.sqrt() using the static method in the Math class.

I've tried to do this in Processing, but the compiler doesn't like it. Something about not being allowed to call a static method from a non-static Papplet class? Does anyone know how to make this work?
Re: static methods in processing
Reply #1 - Oct 13th, 2009, 11:52am
 
try keeping your existing code, and just creating a colorMethods object "myColorProcessor" or what have you -- then call myColorProcessor.complementary(myColor); ...

Or, you can use pure java code by putting it in a tab that ends with .java instead of .pde.
Re: static methods in processing
Reply #2 - Oct 13th, 2009, 11:59am
 
That's not a bad idea.... only takes one extra line to make the object, after all (plus the empty constructor). Thanks for your reply.
Re: static methods in processing
Reply #3 - Oct 13th, 2009, 12:08pm
 
I like your WayFarer game, by the way. As enjoyable and frustrating as the all the Nethack variants, but it's got something fresh about it too.
Re: static methods in processing
Reply #4 - Oct 13th, 2009, 12:09pm
 
Sure -- hopefully someone with more Java experience can explain why you can't just call a class method directly -- my guess is it has to do with everything existing inside PApplet class, as you mentioned.

Edit: Oh hey!  Thank you!
Re: static methods in processing
Reply #5 - Oct 13th, 2009, 12:22pm
 
Giles, your classes are likely not to use Processing methods. So you can just make a class in .java tab/file: it will be in a separate class, not internal to the PApplet.
Re: static methods in processing
Reply #6 - Oct 13th, 2009, 12:50pm
 
Great..I'll try that too..thanks.
Page Index Toggle Pages: 1