[英]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.我想要一个方法
invokeProcessingMethod(String name, Object... args)
从 Processing 中调用一个方法并返回可能产生的任何值。
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我已经有一个方法
invokeMethod(String name, Object...args)
从当前实例上的超类调用一个方法,所以我认为这个实现将是用我已经拥有的方法创建一个草图
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
但我收到以下异常,因为我没有在
setup
或draw
调用 Processing 方法
Exception in thread "main" java.lang.NullPointerException
线程“main”中的异常 java.lang.NullPointerException
at processing.core.PApplet.textWidth(PApplet.java:12960)在 processing.core.PApplet.textWidth(PApplet.java:12960)
at ProcessingRELP.invokeProcessingMethod(ProcessingRELP.java:27)在 ProcessingRELP.invokeProcessingMethod(ProcessingRELP.java:27)
at Test.main(Test.java:25)在 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.您需要先运行草图,因为您正在尝试调用依赖于草图运行时的方法( textWidth()在其计算中使用实时草图的textFont和textSize )而不是静态方法。 Simply instantiating a
ProcessingRELP
object will not run it;简单地实例化一个
ProcessingRELP
对象不会运行它; 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:您需要使用
PApplet.main()
函数:
String[] appletArgs = new String[] { "MySketch" };
PApplet.main("ProcessingRELP");
Shameless self-promotion: here is a tutorial on using Processing as a Java library.无耻的自我推销:这是一个关于使用 Processing 作为 Java 库的教程。
On top of that, I would be very suspicious of your invokeMethod()
approach.最重要的是,我会非常怀疑您的
invokeMethod()
方法。 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.无论哪种方式,我认为您都需要重构代码以使用
PApplet.main()
函数,而不是假设您有对草图本身的引用。
You could do something like move your logic into the setup()
function of your sketch class.你可以做一些事情,比如将你的逻辑移动到你的草图类的
setup()
函数中。 But either way, your Processing sketch needs to be the entry point.但无论哪种方式,您的处理草图都需要成为切入点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.