We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All,
in a little program I'm trying to write, I would like each character of a String
to invoke a method specific to that character. For example, the String
"ABC"
would invoke, in series, methods paintA()
, paintB()
and paintC()
.
I've seen stuff about Reflection in Java, where you can actually invoke methods programmatically, i.e. invoke the method by "reading" its name from a String
, but the code really confuses me... I'm not good enough to understand how this works.
Maybe you have a trick to help achieve something similar, even if less elegant?
Answers
Wow, why was the question posted twice? First time it threw an exception so I though it hadn't gone trhough... sorry. Can the mods please erase the duplicate?
If a function resides at the top, along setup() & draw(), it can be invoked via method() function.
Take a look at this "Multi Page Buttons" example:
Very, very nice, thanks. This makes life way easier! On a different note, I quite like your technique of the
hasClicked()
method in theButton
class!Can you invoke a method using arguments in this way? Say, if the method I would like to invoke looked like
paintA(float x) {stuff}
? How would you write this?Oh I'm just grasping your first reply: it means this wouldn't work for a class method, right?
Unfortunately only "free-roam" Processing functions.
For methods inside classes demand we implement reflection. :-\"
Okay, groping in the dark with an implementation of Reflection with a class method here. I know it's wrong, but I don't know what actually is. Evidently
this
refers to the PApplet (what is a PApplet?) when it should somehow point to theword
object. Can you help further?I've already told you those functions invoked by method() (and btW thread() as well) gotta be "roam" 1s.
In your sketch above, paintX() is a method from class Word.
And therefore, neither method() not thread() can reach that out! :-SS
Move method paintX() outta class Word, so it belongs to the "sketch" class instead. *-:)
P.S.: Add
stroke(-1);
in setup() so those line() statements can actually show up.Okay thanks for your reply. I had got it the first time. It's just, I wish really I could make paintX() a method inside class Word (and thus implement Reflection like you said above). But nevermind, for what I'm intending to do, I can work around this by writing extra code. @-) Thanks!
You can always open a new tab on PDE and move all your "reflection" functions there.
It's as almost as if they were in their own class methinks. :>
Anyways, if you feel that you need those functions to belong to some class, you can't use the convenient method() anymore. You've gotta implement reflection by yourself! :(
You can go w/ getDeclaredMethod(): http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredMethod-java.lang.String-java.lang.Class...-
Or rather go w/ getMethod()
as long as you declare class Word as:public
public class Word {
http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getMethod-java.lang.String-java.lang.Class...-
Some links to help ya out if you decide for this harder approach: :ar!
Thanks for a complete and well informed reply -- like always! :-bd I'm working through the examples and posts you provided. If I get this correctly, Reflection could be seen as treating methods like objects.
Trying to understand the mechanics of this. What should I do so the following does not throw exception NoSuchMethod?
You've got an unhandled compilation error. That means the method you're trying to invoke
throws
some checked Exception. Therefore it demands sometry/catch ()
block to deal w/ it: [-XAnyways, here's a much more detailed getMethod() + invoke() example: :>
This discussion might help.
Thanks for all the time and help, guys. I need time and practice to wrap my head around this.