20180302

Using Processing from Scala

Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts.

I had always wanted to play with Processing (after leafing through The Processing Handbook and Visualizing Data some years ago). My general dislike for Java or the JVM made me just play a very short amount of time with processing.js something around 2011 (there was a native processing.js application for iOS, I used it for a while on my iPad and iPod Touch).


But, times have change and I’ve been working with Scala for 2 years already. Since Processing is built on top of Java, using it from Scala should be easy (or doable), but finding details (specially not involving using IntelliJ or other IDEs) is tricky. I could not find anything useful so I had to patch it up from several sources.

These are the steps I had to follow to get it running on Mac OS, with Java 8 and Scala 2.12 (the version of Scala is not that important though, and after all is set in the SBT build).

Clone my sample project

You can find a minimal sample project here. I’m using Spire for its implementation of complex operations (also, I have wanted to try this library for a while), so this comes as an extra dependency in addition to the OpenGL bindings for Java.

You can run sbt update while you do all other steps, since fetching the dependencies can always take a while.

This will draw the Mandelbrot set using the Distance Estimation Method. I first found this algorithm in The Beauty of Fractals and have liked ever since. It is the algorithm used for the header on this blog and on my LinkedIn profile. A pity it’s not as easy (or even possible) to derive for entire transcendental functions, otherwise I would have used it for my paper.

It's a straight implementation to show a bit of Processing and have a nice image, but it has no antialiasing, no controls for iterations or parameter tweaking. Zoom and adjustments are automatically picked by a really dumb scaling (it shows as you start zooming) and there is no colour palette (nothing prevents adding it except that it would add complications).

It's not the nicest Scala ever. Processing is based on Java, and to get some things working mutability seems to be the best approach. For now.

Install the native libraries for jogamp

Jogamp is the set of multimedia API bindings for Java (binds to OpenGL, for instance, with the jogl sub-package). For Processing we need to install the native libraries (jnilibs in the case of Mac OS, dlls for Windows, so for other Linux systems) into our Java classpath.

To do so, first download the full jogamp 7zipped package from here. On uncompress, move all jnilibs in the lib/macosx-universal folder to somewhere in your Java library path so Java can find them (or, alternatively, change your path). You can easily look which paths form it by firing a Scala REPL and running
System.getProperty("java.library.path")

In my system this points to
~/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
and I put them in the first (since it seems user specific)

Download Processing

You need the core library from Processing in jar format to use in your builds. Just download the processing app, and get core.jar from the Contents/Java folder in the application bundle. Place it into your lib folder.

Although I could add it to the Github project (this core is LGPL), I prefer leaving this as a manual step to make sure anyone trying always has the latest version of Processing.

Ready to go

sbt run

If everything went fine you should now see the following (after some time computing):


And, if you click randomly a few times


You can also try to find the magical command that gets you a coloured gradient




Written by Ruben Berenguel