0

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 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for that, @MikePomaxKamermans - thanks to your answer, I simply added import peasy.*; to the .pde (see my post below), and now the example in the OP compiles without a problem. "Why not just do this entirely in Processing instead" - because I want to provide an alternate class to one of the peasycam classes, which will mostly be a copy of it; so it would be more convenient for me to make the few changes there in Java directly. Thanks again - cheers!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.