Loading...
Logo
Processing Forum
Hi,
I've written some Java programs using the processing.core.* libraries (i.e. I compile them with the Java compiler, not the Processing IDE).  I would like to embed this code into a Java applet so that the Processing sketches can be viewed and manipulated with a web browser.  Some of the Processing documentation indicates that it is no longer possible to export Processing sketches to a Java applet, but I'm hoping this just means that exporting isn't supported by the Processing IDE.  Is it possible to use the Processing libraries in a Java applet?  Has someone written something about how to do this?

Replies(3)

Short answer: download Processing 1.5.1, export an applet with it, study the generated / included jars and the generated HTML file. It will show you how it was done. Not very hard.

The general idea is to bundle the sketch as a jar, and to declare the applet properly in the HTML page, pointing both to the generated jar and the core.jar that can be found in the Processing installation dir. And perhaps auxiliary jars, depending on the core libraries you use. Of course, these jars must be beside the sketch one on the Web server.
Yes, you can certainly do that, and it should work with any version of processing if you can get the libraries to compile in your Java editor (outside of Processing IDE), you should be able to build a Jar out of it. Eclipse should do this easily. Read this ->  http://processing.org/tutorials/eclipse/
 
What I like to do and is really cool and super easy is to just run the PDE directly in an HTML 5 capable browser using processing.js. It turns an HTML 5 canvas into a processing sketchpad! Simply put any processing code file (.pde) in a web folder with this simple web page:
 
<html><head><script src=" http://processingjs.org/js/processing.min.js" type="text/javascript"></script></head>
<body><center><canvas data-processing-sources="myFile.pde"></canvas></body></html>
 
Replace the data-processing-sources="myFile.pde" with your file name of course. Also, put processing.js in the folder if you do not want to link externally. It is included in your "Processing\modes\JavaScriptMode\template" folder if you install the "JavaScript" mode. This is your sketch folder - not the processing install folder. Also, there are more options to the HTML page, if you export to JavaScript from inside processing it will make a more complex HTML page, but I've had no problems with my condensed version, which is much easier to change for different scripts. I even made a page that dynamically changed PDEs with a menu. BTW, you can edit the pde and refresh the page, no need to re-export from processing, I open my PDEs directly on the web server, so in processing all I have to do is save, then refresh the webpage!
 
Even better still, you can put EVERYTHING in a single HTML file, like this little drawing program:
 
<html><head><script src=" http://processingjs.org/js/processing.min.js" type="text/javascript"></script></head>
<body><center><canvas id="pdeCanvas"></canvas></body></html>
<script type="text/processing" data-processing-target="pdeCanvas">
void setup() {
  size (800, 600);
  background(128);
}
void draw() {
  if(mousePressed) line(pmouseX, pmouseY, mouseX, mouseY);
}
</script>
Interesting information, paulr451, but since the topic was about using Java libraries in applets, PJS isn't really usable here...