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 › Integrate PDF sketch with Java
Page Index Toggle Pages: 1
Integrate PDF sketch with Java (Read 1722 times)
Integrate PDF sketch with Java
Apr 14th, 2008, 6:44pm
 
Hi,

I'm using Processing within a larger Java application to write a PDF file. The problem I have is that the sketch seems to terminate without error when creating the file, i.e. when executing this line:

Code:
size(200,200, PDF, "d:/tmp.pdf") 



If I rewrite the sketch just to display the output instead (e.g. size(200,200)), everything seems to work fine.

I'm developing in Eclipse and I've got core.jar, pdf.jar, and itext.jar on my build path. The calling code is within a JUnit test which calls sc.someMethod(); (where sc is an instance of SomeClass).

Code:

public class SomeClass {

private MyModel model; // contains data used in sketch

public void someMethod() {
LayoutGraph.setModel(model);
LayoutGraph.main(new String[]{});
}

}


and the sketch itself (bare bones) is

Code:

public class LayoutGraph extends PApplet {
private MyModel model;
int myField = model.getFieldValue();
// etc

static public void main(String args[]) {
PApplet.main(new String[]{LayoutGraph.class.getName()};
}

public void setup() {
size(200,200, PDF, "D:/tmp.pdf");
// more set up
}

public void draw() {
background(255);
// more drawing stuff
exit();
}

public void setModel(MyModel model) {
LayoutGraph.model = model;
}

}


Again this code terminates without error but doesn't get beyond the size() declaration (it does touch and create the PDF in the right place but nothing's written into it). The reference to the model instance works fine and all the fields have sensible values.

Any ideas how I can get this working? Thanks.
Correct way to call sketch from Java application
Reply #1 - Apr 22nd, 2008, 3:36pm
 
Since there haven't been any replies to this post yet, I thought I'd ask the question in another way.

What is the 'right' way to call a Processing sketch from a Java application? E.g.

Code:

public class MyApplication {
static public void main(String[] args0) {
// do some stuff

????
// Call to Processing sketch
// This may or may not actually show the applet
// At the moment I'm just using it to write a PDF file.

// do some more stuff
}
}


With the whole lot called, after compilation, as
Code:

java MyApplication


Thanks.
Re: Integrate PDF sketch with Java
Reply #2 - Apr 22nd, 2008, 6:04pm
 
you should extend the PApplet
Re: Integrate PDF sketch with Java
Reply #3 - Apr 22nd, 2008, 7:57pm
 
Thanks for the response but in the first post, you'll see that I have extended PApplet. The problem is how to call a sketch that extends PApplet from another application and have it return predictably.
Re: Integrate PDF sketch with Java
Reply #4 - Jun 8th, 2008, 11:58pm
 
You might find <a href="http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Collaboration;action=display;num=1210808972;start=2#2">this post by Ira Greenberg</a> in which he mentions and makes available <a href="http://www.friendsofed.com/extras/159059617X/Integrating-Processing-Within-Java.pdf">here</a>, which is a GREAT introduction to doing processing in a java application.
Re: Integrate PDF sketch with Java
Reply #5 - Jun 8th, 2008, 11:58pm
 
Sorry, I'm not used to YABB syntax.  Those links are:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Collaboration;action=display;num=1210808972;start=2#2
http://www.friendsofed.com/extras/159059617X/Integrating-Processing-Within-Java.pdf
Re: Integrate PDF sketch with Java
Reply #6 - Jun 9th, 2008, 2:55pm
 
the original code, as posted should be ok, aside from the "d:/tmp.pdf" syntax, which i'm not sure whether it works ("d:\\tmp.pdf" would be better).

for what it's worth there's no point in calling:
LayoutGraph.main(new String[]{});
just call PApplet.main() from that point instead of calling the main() inside LayoutGraph which does the same thing.

i've occasionally seen a problem where a pdf-based sketch terminates before it has finished writing, though i haven't been able to track that down consistently.

does the code work from inside processing itself?
Re: Integrate PDF sketch with Java
Reply #7 - Jun 11th, 2008, 1:29pm
 
tacitdynamite: Thanks for the link. I've seen that before but the problem is in closing the app. It calls System.exit(0), which I'm trying to avoid in my application (see this thread)

fry: yes the code works within processing. My problem though is more generally about how to get called sketches to return without killing the JVM (again see the thread above).

At the moment I've got this ugly solution, using a controller class to start the sketch in its own Thread group. The controller can then be called to kill the entire thread group of the sketch. However it only seems to work with the stupid sleep command; I've tried adding a "finished" flag to the called sketch but no luck (note the blocked out chunk of code).

Code:

public class ProcessingController {

int timeout = 20000;
Process p = null;
String classname = null;


/**
* Class constructor specifying class name of sketch
*
* @param classname
*/

public ProcessingController(String classname) {
this.classname = classname;
}


/**
* Draws the sketch
*/
public void draw() {
try {
p = ProcessingController.startAnotherClassInItsOwnThread(classname, new String[]{});
} catch (Exception e) {
e.printStackTrace();
}

// Necessary ugliness starts here
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}

// Preferred solution which doesn't work
/* while (!PDFGraph.isDone()) {}
* if (p!=null) {
* p.terminate();
* }
*/

p.terminate();

}


private static Process startAnotherClassInItsOwnThread(String classname, String[] args) {
Process process = new Process(classname, args);
ThreadGroup threadgroup = new ThreadGroup("main");
Thread thread = new Thread(threadgroup, process);
thread.start();
return process;
}
}



This calls the Process class which actually starts the sketch. The terminate method uses the deprecated stop() to stop the ThreadGroup held by the Process.

This all seems way too complicated. All I want to do is:
- start a sketch from within a program
- check is that sketch is finished
- once it's finished, stop all the processing stuff (not the JVM) and return nicely to the calling program

Thanks!
Re: Integrate PDF sketch with Java
Reply #8 - Jun 11th, 2008, 2:46pm
 
jkeirstead wrote on Jun 11th, 2008, 1:29pm:
fry: yes the code works within processing. My problem though is more generally about how to get called sketches to return without killing the JVM (again see the thread above).

if you don't want the vm to quit, don't call exit().
Re: Integrate PDF sketch with Java
Reply #9 - Jun 11th, 2008, 2:58pm
 
Sorry I must be missing something. How do I get the sketch to stop without calling exit? (in an automated sketch, which doesn't display to the user but writes to file, and uses the draw loop)
Page Index Toggle Pages: 1