We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › What does exit() do (and can it please stop!)
Page Index Toggle Pages: 1
What does exit() do? (and can it please stop!) (Read 851 times)
What does exit() do? (and can it please stop!)
May 19th, 2008, 11:28pm
 
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.
Re: What does exit() do? (and can it please stop!)
Reply #1 - May 19th, 2008, 11:41pm
 
I'd suggest using the beginRecord/endRecord method for generating PDFs rather than the createGraphics one, that way you have more control over when it stops/starts, and don't have to worry about exit() etc.
Re: What does exit() do? (and can it please stop!)
Reply #2 - May 20th, 2008, 10:41am
 
Thanks for the tip but that doesn't work for me. It still won't close the PDF file without a call to exit() (as the documentation suggests, since size() is used instead of createGraphics). Any other ideas?

The new code is:

Code:

public void setup() {
size(400,400);
beginRecord(PDF, "output.pdf");
}


public void draw() {
PFont font = createFont("Arial", 32);
textFont(font, 12);

background(128, 0, 0);
line(50, 50, 250, 250);
text("Test", 0, height/2);
endRecord();
exit();
}
Re: What does exit() do? (and can it please stop!)
Reply #3 - May 20th, 2008, 12:52pm
 
You don't need the exit(), seriously.

Code:
import processing.pdf.*;

PFont f;
void setup()
{
size(400,400);
f=createFont("Arial",32);
fill(0);
}

void draw()
{
background(128,128,128);
textFont(f,12);
text(frameCount,0,20);
if(frameCount==200)
doPDF();
}

void doPDF()
{
beginRecord(PDF,"myfile.pdf");
background(128,0,0);
line(50,50,250,250);
textFont(f,12);
text("Test",0,height/2);
endRecord();
}


This shows that you don't need to exit to get the PDF saved. On frame 200 it creates your PDF image, but carries on running and the PDF is saved and uncorrupt.
Re: What does exit() do? (and can it please stop!)
Reply #4 - May 20th, 2008, 1:15pm
 
Thanks John. That works great.

The other part of the problem though is that

a) I don't want to see the applet (hence the createGraphics stuff)

b) I don't want the applet to keep running after drawing the pdf. (But at the same time, I do want the calling program to keep going - the exit() problem)

Do you have any ideas on how I could get these two things working?
Stop processing thread
Reply #5 - May 21st, 2008, 12:51pm
 
I've answered one of my one questions since the following code will draw the sketch without looking at it and without using createGraphics.

Code:

size(200,200,PDF,"myfile.pdf")
// beginRecord(PDF, "myfile.pdf") don't need this
endRecord()


I still don't know how to stop the thread with the sketch though. Exit() stops the whole VM; I just want to stop the thread drawing the sketch so that after writing all the numbers, the programme will exit normally.
Page Index Toggle Pages: 1