Backspaces

October 17, 2006

Processing: .. with Eclipse

Filed under: Hacks — backspaces @ 4:49 pm

We at Redfish have been using Processing.org’s graphics system for models requiring fairly sophisticated 3D capabilities. Here’s an example prototype of the Pittsburgh PNC Baseball Stadium done with Processing. (Note: Our Redfish site has more information on the project page.

Processing is more than a graphics library, it has a delightful IDE which makes Java much easier, especially for the Java novice. The IDE has great tools such as a web-page builder with the project as an applet. It also can build an application version for Mac, Windows and Linux.

One difficulty we faced however, was that we wanted to commit to fairly complicated models that would benefit from Java 1.5 (Processing uses an earlier version) and an IDE familiar to Java programmers. We also are interested in interfacing Java to other languages such as Groovy, Python/Jython, Ruby/JRuby, and even JavaScript/Rhino.

So I decided to experiment with Processing, using Eclipse and Java 1.5. To do this, I made two models: RandBoxes, which is really simple .. just showing 1000 boxes and randomly jiggling them each step. The second model, RoadGrid, is more complicated: it draws a rectangular road grid and has “cars” (rectangles) moving along the roads. At each intersection, the cars randomly chose a new road segment.

By a very fortunate coincidence, the Processing team had just done a fairly major cleanup, making their system work nicely in Eclipse. So I built a new Eclipse project, including the Processing core.jar file and the Processing JavaDocs, and using the Java 1.5 SDK.

The Processing IDE puts its code fragments into .pde (Processing Development Environment) files which the web-page builder includes in the applets above. Here’s the RandBoxes .pde file. Note that there is no Class defined, nor imports. The Processing IDE handles that for you by generating a java file automatically. Here’s the RandBoxes.java file before and after formatting by Eclipse. Note how the IDE “wraps” the .pde file in a class and adds a Main(), as per the Java norm. It also does other preprocessing like changing the “color” type to a Java “int”.

Well, imaging my surprise when after dumping the above file into Eclipse, although it had many warnings, it none the less compiled and ran, both as an applet and as an application! Here’s what Eclipse looked like:
Eclipse Processing Warnings

The next step was to upgrade the .java file to be 1.5 savy: removing unnecessary imports, migrating to generics, using the for/in loop and so on. This resulted in the RandBoxes1.java file.

Some code examples:

Using generics, this:

ArrayList cars = new ArrayList();
for (int i = 0; i<cars.size(); i++)
     ((Agent) cars.get(i)).step();

.. converted to:

ArrayList<Agent> cars = new ArrayList<Agent>();
for (Agent car : cars) car.step();

Converting to the new printf() facility, using a “static import”, this:

println("frameRate="+frameRate
        +" frameCount="+frameCount);

.. converted to:

import static java.lang.System.out; // NOTE Static Import
...
out.printf("frameRate=%.2f frameCount=%dn",
            frameRate, frameCount);

Another nifty 1.5-ism was using Generic methods. Their syntax is a bit odd, but it sure is great not having to use casts all over the place! Here’s an example taken from the second demo, RoadGrid. This old-style method:

public Object randomOneOf(ArrayList a) {
    return a.get(randomInt(a.size()));
}

now looks like:

public <T> T randomOneOf(ArrayList<T> a) {
    return a.get(randomInt(a.size()));
}

After “modernizing” to 1.5, Eclipse shows no warnings:
Eclipse Processing Update to 1.5

So Processing with Eclipse is working like a charm, and lets us use the latest Java version, use the JavaDocs easily, and even browse the Processing code. And with a little hand tweaking of Processing’s IDE-generated web page, we can even use the new Java 1.5 applet.

One downside: many browsers are not yet using 1.5 so will not be able to view the newer version. But a solution is RetroWeaver which converts a 1.5 class back to its 1.4 format, thus potentially letting us use Eclipse, Java 1.5, Processing .. yet deploying for 1.4 browsers.

We’ll follow up shortly with some other experiments with Processing with other languages .. stay tuned!

5 Comments »

  1. [...] Processing with EclipseIf you are an avid Java programmer and have been using Eclipse for your daily routine this would be best way to use Processing, making your workspace comfortable and familiar. Another benefit that Eclipse adds is that you can harness the power of the latest Java IDE. [...]

    Pingback by Beautiful Motion Graphics Created With Programming: Showcase, Tools and Tutorials | Web Design Cool — February 8, 2010 @ 8:27 pm

  2. [...] Processing with Eclipse If you are an avid Java programmer and have been using Eclipse for your daily routine this would be best way to use Processing, making your workspace comfortable and familiar. Another benefit that Eclipse adds is that you can harness the power of the latest Java IDE. [...]

    Pingback by Beautiful Motion Graphics Created With Programming: Showcase, Tools and Tutorials « Top 10 Web Hosting — February 9, 2010 @ 5:23 pm

  3. [...] Processing with EclipseIf you are an avid Java programmer and have been using Eclipse for your daily routine this would be best way to use Processing, making your workspace comfortable and familiar. Another benefit that Eclipse adds is that you can harness the power of the latest Java IDE. [...]

    Pingback by Beautiful Motion Graphics Created With Programming: Showcase, Tools and Tutorials | Variedades e Bookmarks — February 11, 2010 @ 8:16 pm

  4. Caricatures Drawing – How to Draw a Caricature Picture…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]…

    Trackback by marker ski bindings — April 23, 2012 @ 11:54 pm

  5. [...] Processing with EclipseIf you are an avid Java programmer and have been using Eclipse for your daily routine this would be best way to use Processing, making your workspace comfortable and familiar. Another benefit that Eclipse adds is that you can harness the power of the latest Java IDE. [...]

    Pingback by Beautiful Motion Graphics Created With Programming: Showcase, Tools and Tutorials | Smashing Magazine — April 28, 2012 @ 6:39 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress