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 › Is there a function pointer type in Processing
Page Index Toggle Pages: 1
Is there a function pointer type in Processing? (Read 1057 times)
Is there a function pointer type in Processing?
Mar 30th, 2009, 7:26am
 
Hi,

Newb asking for some help. I wrote my own version of buttons. I'd like to be able to line the buttons to their functions by assigning a function pointer as a property of the class. Is there a way to do it? I'm experienced in C but not familiar with Java. Thank you.
Re: Is there a function pointer type in Processing?
Reply #1 - Mar 30th, 2009, 8:51am
 
There is 'java.lang.reflect.Method' with an 'invoke' call.

Searching Google for 'java.lang.reflect.Method' should give you a few examples.

Also, if you are brave, you can see examples of this used in processing source (off hand I know it is used in Video and Serial libraries).
Re: Is there a function pointer type in Processing?
Reply #2 - Mar 30th, 2009, 9:04am
 
You don't do it this way in Java...
I won't recommend using reflection, it is too convoluted for your needs, and not really in Java spirit.
You should read a bit on classes (there is a tutorial about it). The idea is to make a generic button class, providing generic behavior, then override it for each button, defining specific behavior.
Or you define listeners, but it is a bit more complex.
Re: Is there a function pointer type in Processing?
Reply #3 - Mar 30th, 2009, 12:34pm
 
Is there a way to use generic buttons without creating subclasses for each function? That is, without listeners...

I came from visual basic myself. I used reflection to implement the 'buttonname_click()' style in a custom GUI. Only buttons and checkboxes though.

Re: Is there a function pointer type in Processing?
Reply #4 - Mar 30th, 2009, 1:05pm
 
anonymous classes are a way to do that:

Code:

class Button
{
ButtonWorker worker;

Button ( ButtonWorker w ) {
worker = w;
}

void press () {
worker.action();
}
}

class ButtonWorker
{
void action () {}
}

Button a, b;

void setup ()
{
a = new Button( new ButtonWorker() {
void action () {
println("button pressed .. i think i'm 'A'");
}
} );

b = new Button( new ButtonWorker() {
void action () {
println("another button pressed .. i think i'm 'B'");
}
} );

a.press();
b.press();
}


and then there are interfaces ...

F
Re: Is there a function pointer type in Processing?
Reply #5 - Mar 30th, 2009, 8:02pm
 
fjen, thank you. Your method worked. I didn't know there exists such a "new object{method(){//blah blah}}" syntax. What's that way called, anonymous class (referring to ButtonWorker?)?

Philho, thank you. I'm not OOP adept. Is this what you meant?class
buttons {

public action () {
//do something
 }

}

class startButton extends buttons {

public action () {
println ("This starts the program.");
 }
}

startButton myButton;
myButton=new startButton();
myButton.action();
Re: Is there a function pointer type in Processing?
Reply #6 - Mar 30th, 2009, 11:03pm
 
no the anonymous class is inside the new Button( .. ) statement. ButtonWorker is the super class of the anonymous class i'm creating inline there.

here's more:
http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm

F
Re: Is there a function pointer type in Processing?
Reply #7 - Mar 31st, 2009, 3:28am
 
I am not big fan of anonymous classes, but they can be simpler when the action is short like here. It is common practice in Java's Swing. They are often used to redirect action to some other method.

And yes, I meant something like that, with the idea of putting some common code in the generic class, which can be abstract.
Page Index Toggle Pages: 1