Processing in Clojure

While reading the article Scala + Processing – an entartaining way to learn a new language I came across Processing“an open source programming language and environment for people who want to create images, animations, and interactions.”

The author of the article found Processing an excellent tool to learn Scala the language, that in turn encouraged me to try it out with Clojure. I struggled with a few “deficiencies” of Clojure that made me suffer a great pain when my object-oriented thinking about state management was constantly strained as well as was extensively used in the examples I relied upon while Clojure’ing ’em.

Below’s my take on the fine example of using Processing to create “sketch [that] will be drawing random grayscale flowers under mouse cursor.” I’d appreciate any tips and tricks to have it a top-notch implementation I could share during my presentation about Clojure at InfoShare 2011.

I’d like to thank Valentin Simonov for his article about Processing and Scala. The reading’s very enlightening and let me appreciate simplicity of each.

Run the application with /Applications/ in Clojure’s CLASSPATH.

(import [processing.core PApplet])

(def angle (atom 0))

(def random (java.util.Random.))

(def papplet
  (proxy [PApplet] []
    (PApplet [] (println "in constructor"))
    (setup []
      (doto this
        (.size 640 360)
        (.background 102)
        (.fill 102)))
    (draw []
      (let [_ (swap! angle + 10)
            value (* (Math/cos (Math/toRadians @angle)) 6)
        (loop [a 0]
          (if (< a 360)
            (let [xoff  (* (Math/cos (Math/toRadians a)) value)
                  yoff  (* (Math/sin (Math/toRadians a)) value)]
                  (doto this
                    (.fill (. random nextInt 255))
                    (.ellipse (+ (. this mouseX) xoff) (+ (. this mouseY) yoff) value value))
                  (recur (+ a 75)))))
        (doto this
          (.fill 255)
          (.ellipse (. this mouseX) (. this mouseY) 2 2))))))
(.init papplet)

(def frame (javax.swing.JFrame. "Test"))

(doto frame
  (-> (.getContentPane) (.add papplet))
  (.setVisible true))
Be Sociable, Share!
This entry was posted in Languages.

4 Responses to Processing in Clojure

  1. Brian Cooley says:

    You might want to check out

    Incanter ( also wraps processing for use in Clojure.

  2. Pingback: “Processing in Clojure” Made Functional « Squirrel's

  3. Konrad Garus says:

    The reply is too long for a comment so I posted it on my blog:

    Hope it helps. Sorry for being pedantic, but I felt invited to nitpicking. :-)

  4. Valentin says:

    Jacek, Konrad cool stuff q: too bad I can’t stand nested brackets o.O

Leave a Reply

%d bloggers like this: