使用Processing库 - 在Processing sketch中的Java文件中?

时间:2014-11-05 00:02:35

标签: java processing

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 - 如果我从import peasy.*;草图中执行.pde,它的工作正常。

我想,这与路径有关 - 在草图中显然是纯Java文件,不要在草图中引用与.pde文件相同的库路径。

是否可以使用import peasy.org...行编译此草图(除了,我猜,复制/符号化草图文件夹中的peasycam库,此处为/tmp/Sketch/< ---编辑:刚刚按照描述尝试了符号链接,它不起作用;报告了相同的错误)?

2 个答案:

答案 0 :(得分:1)

这是您了解Processing不是实际 Java的地方,它只是具有类似的(ish)语法,可以通过将所有.pde文件聚合到一个类中来在JVM中运行其代码可以编译为在JVM上运行。 Processing有处理导入等的规则。

为什么不在处理中完全执行此操作呢?

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库中?

答案 1 :(得分:0)

好的,感谢@MikePomaxKamermans回答,特别是“通过将所有.pde文件聚合到一个类”,我只是尝试在.pde文件中导入peasy之前第一次提到foo;也就是说,在/tmp/Sketch/Sketch.pde我现在有:

// forum.processing.org/two/discussion/3677/
// usage-class-from-java-file-is-there-a-full-doc-for-that

import peasy.*; // add this

Foo tester;
...

...然后草图编译没有问题(但请注意:虽然这种方法适用于此示例,但它在原始问题中不起作用,驱使我发布问题)。