Hi,
I posted a couple weeks ago about integrating a processing PDF sketch with Java and didn't get the answers I was hoping for. Having played around some more, I'm getting closer to the problem but could still use some help (please).
Here's my sketch code:
Code:
public class PDFGraph extends PApplet {
public void setup() {}
public void draw() {
PGraphics pdf = createGraphics(300, 300, PDF, "output.pdf");
PFont font = createFont("Arial", 32);
pdf.beginDraw();
pdf.textFont(font, 12); // This has to be after beginDraw
pdf.background(128, 0, 0);
pdf.line(50, 50, 250, 250);
pdf.text("Test", 0, height/2);
pdf.dispose();
pdf.endDraw();
exit(); // this line is the problem
}
}
And the sketch is called by:
Code:
public static void main(String[] args) {
Thread t = new Thread(){
public void run() {
PApplet.main(new String[] { "--present", "--hide-stop",
PDFGraph.class.getName() });
}
};
t.start();
// Testing for thread
for (int i=0; i<1000000; i++) {
System.out.println(i);
}
}
The problem is as follows:
Expected behaviour: Thread t starts up, draws the sketch, quits, and the main() method continues by writing a bunch of numbers.
Actual behaviour: Thread t starts up, draws the sketch, quits taking the whole application with it (i.e. first couple hundred numbers are written but then stops).
Having read the dev docs, it seems like I should be able to drop the exit() call from the sketch and still close the file properly via endDraw() and dispose(). But this doesn't happen: I just get a corrupt 0 KB pdf file. The exit() method apparently has to be called and I can't seem to catch it with a thread of its own. The documentation really isn't clear on what exit() does, especially when it comes to finishing the writing of a PDF file.
Can someone please explain how I can write a PDF file and have my code continue running? (Preferably without having to implement a SecurityManager or something).
Many thanks,
James
PS A point of interest. When using PGraphics, the call to textFont() must be made
after beginDraw(). This isn't mentioned in the documentation.