有点像How to use public class frome .java file in other processing tabs?的后续;使用 Usage class from .java file - is there a full doc for that? - Processing 2.x and 3.x Forum 中的示例,我有这个:
/tmp/Sketch/Sketch.pde
// forum.processing.org/two/discussion/3677/
// usage-class-from-java-file-is-there-a-full-doc-for-that
Foo tester;
void setup() {
size(600, 400, JAVA2D);
smooth(4);
noLoop();
clear();
rectMode(Foo.MODE);
fill(#0080FF);
stroke(#FF0000);
strokeWeight(3);
tester = new Foo(this);
tester.drawBox();
}
/tmp/Sketch/Foo.java
import java.io.Serializable;
//import peasy.org.apache.commons.math.geometry.Rotation;
//import peasy.org.apache.commons.math.geometry.Vector3D;
import processing.core.PApplet;
import processing.core.PGraphics;
public class Foo implements Serializable {
static final int GAP = 15;
static final int MODE = PApplet.CORNER;
final PApplet p;
Foo(PApplet pa) {
p = pa;
}
void drawBox() {
p.rect(GAP, GAP, p.width - GAP*2, p.height - GAP*2);
}
}
该示例按原样运行良好 - 但如果我取消注释 import peasy.org...
行,则编译失败并显示:
The package "peasy" does not exist. You might be missing a library.
Libraries must be installed in a folder named 'libraries' inside the 'sketchbook' folder.
当然,我确实在 /path/to/processing-2.1.1/modes/java/libraries/peasycam/
下安装了 PeasyCam - 如果我执行 从
。.pde
草图中导入 peasy.*;
我猜,这与路径有关 - 显然草图中的纯 Java 文件,不引用与草图中的 .pde 文件相同的库路径。
是否可以使用 import peasy.org...
行编译此草图(我猜,除了复制/符号链接(symbolic link)中的 peasycam
库sketch 文件夹,此处 /tmp/Sketch/
<--- 编辑:刚刚按照描述尝试符号链接(symbolic link),它不起作用;报告了相同的错误)?
最佳答案
在这里您可以了解到,Processing 实际上并不是 Java,它只是具有类似的语法,并且可以通过将所有 .pde 文件聚合到一个类中来在 JVM 中运行其代码可以编译为在 JVM 上运行。处理有自己的处理导入等的规则。
为什么不完全在处理中完成此操作?
class Foo {
static int MODE = ...;
static int GAP = ...;
PApplet sketch;
public Foo(PApplet _sketch) {
sketch = _sketch;
...
}
void drawBox() {
sketch.rect(GAP, GAP, p.width - GAP*2, p.height - GAP*2);
}
...
}
然后确保将其放在文件 Foo.pde
中或与您的草图位于同一目录中的其他内容中,并通过常规处理导入机制将您的草图加载到 peasy 库中?
关于java - 使用处理库 - 在处理草图的 Java 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26747409/