簡體   English   中英

使用處理庫-在處理草圖的Java文件中?

[英]Using Processing libraries - in a Java file in a Processing sketch?

如何在其他處理選項卡中使用.java文件中的公共類的后續措施 ; 使用來自.java文件的Usage類的示例-是否有完整的文檔? -處理2.x和3.x論壇 ,我有這個:

/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.*; peasy,它可以正常工作import peasy.*; .pde草圖中.pde

我猜想,這與路徑有關-顯然草圖中的純Java文件與草圖中的.pde文件引用的庫路徑不同。

是否有可能使此草圖與import peasy.org...行一起編譯(我想,除了/在草圖文件夾中復制/符號鏈接peasycam庫,在這里/tmp/Sketch/ <---編輯:只是如所描述的嘗試了符號鏈接,它不起作用;報告了相同的錯誤)?

在這里,您可以了解到Processing 實際上不是Java,它具有類似的語法,並且可以通過將所有.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庫中?

好,感謝@MikePomaxKamermans回答,尤其是“ 通過整合所有.pde文件合並為一個班 ”,我只是試圖導入peasy第一參考為foo之前在.pde文件; 也就是說,在/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;
...

...然后可以毫無問題地編譯草圖(但請注意:盡管此方法適用於本示例,但在導致我提出問題的原始問題中不起作用)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM