简体   繁体   中英

How can I invoke a Processing method from outside a Processing sketch?

I want to have a method invokeProcessingMethod(String name, Object... args) that invokes a method from Processing and returns any value that may result.

I already have a method invokeMethod(String name, Object...args) that invokes a method from a superclass on the current instance, so I thought an implementation of this would be create a sketch with the method I already have

 class ProcessingRELP extends PApplet{

       public static void main(String[] args) {
            PApplet.main("ProcessingRELP");
       }

       void settings(){

       }

       void setup() {

       }

       void draw() {

       }

       invokeProcessingMethod(String name, Object... args) {
             invokeMethod(name, args);
       }
 }

and then do something like

 class Test {
       public static void main(String[] args) {
            ProcessingRELP sketch = new ProcessingRELP();
            Object data = sketch.invokeProcessingMethod("textWidth", "hello");
       }
 }

but I get the following exception because I am not invoking the Processing method in setup or draw

Exception in thread "main" java.lang.NullPointerException
at processing.core.PApplet.textWidth(PApplet.java:12960)
at ProcessingRELP.invokeProcessingMethod(ProcessingRELP.java:27)
at Test.main(Test.java:25)

Is there anyway to invoke a Processing method outside the sketch or a creative way to still do it in the sketch but be able to retrieve the data from outside the sketch?

You need to run the sketch first, because you are trying to invoke methods that depend on the runtime of the sketch ( textWidth() uses the textFont and textSize of a live sketch in its calculation) and not static methods. Simply instantiating a ProcessingRELP object will not run it; this can be done with the following:

ProcessingRELP sketch = new ProcessingRELP();
PApplet.runSketch(new String[]{"--location=0,0", ""}, sketch);

Now you are able to call your method since the sketch is running.

I don't know of a way to initialize a Processing sketch in a way that gives you a reference to it. You need to use the PApplet.main() function:

String[] appletArgs = new String[] { "MySketch" };
PApplet.main("ProcessingRELP");

Shameless self-promotion: here is a tutorial on using Processing as a Java library.

On top of that, I would be very suspicious of your invokeMethod() approach. Why can't you just invoke the function directly?

Something like this:

float stringWidth = sketch.textWidth("hello");

Either way, I think you're going to need to refactor your code to use the PApplet.main() function instead of assuming you have a reference to the sketch itself.

You could do something like move your logic into the setup() function of your sketch class. But either way, your Processing sketch needs to be the entry point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM