0
votes

Somewhat of a followup to How to use public class frome .java file in other processing tabs?; using the example from Usage class from .java file - is there a full doc for that? - Processing 2.x and 3.x Forum, I have this:

/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);
  }
}

The example runs fine as is - but if I uncomment the import peasy.org... lines, then compilation fails with:

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.

Of course, I do have PeasyCam installed, under /path/to/processing-2.1.1/modes/java/libraries/peasycam/ - and it works fine, if I do a import peasy.*; from a .pde sketch.

I guess, this has something to do with paths - apparently pure Java files in a sketch, do not refer to the same library paths as .pde files in a sketch.

Is it possible to get this sketch to compile with the import peasy.org... lines (other than, I guess, copying/symlinking the peasycam library in the sketch folder, here /tmp/Sketch/ <--- EDIT: just tried symlinking as described, it doesn't work; the same error is reported)?

2

2 Answers

1
votes

This is where you learn that Processing isn't actually Java, it just has a similar(ish) syntax and can run its code in the JVM by aggregating all .pde files into a single class that can be compiled for running on the JVM. Processing has its own rules for dealing with imports and the like.

Why not just do this entirely in Processing instead?

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);
  }
  ...
}

and then make sure to have that in a file Foo.pde or something in the same dir as your sketch, with your sketch loading in the peasy library through the regular Processing import mechanism?

0
votes

Ok, thanks to @MikePomaxKamermans answer, especially "by aggregating all .pde files into a single class", I simply tried importing peasy in the .pde file before the first reference to foo; that is, in /tmp/Sketch/Sketch.pde I now have:

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

import peasy.*; // add this

Foo tester;
...

... and then the sketch compiles without a problem (but note: while this approach works for this example, it somehow didn't work in the original problem that drove me to post the question).