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/Processing.app/Contents/Resources/Java/core.jar 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) (.smooth) (.noStroke) (.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)) (.pack) (.setVisible true))