clj-processing

cranebird2009-12-16


clj-processing 環境が壊れていたので作り直し。ant だとかの java 資産が使えるのが clojure のいいところ。

芸術度は低すぎるが一応張っておこう。emacs から clojure の repl を実行した後、以下をロードすると Processing (PApplet) のウィンドウが立ち上がり、draw メソッドが呼ばれ続ける。

(ns p5
  (:use rosado.processing)
  (:import (javax.swing JFrame JLabel JTextField JButton WindowConstants))
  (:import (processing.core PApplet)))

(defn lazy-seq-fibo
    ([]
        (concat [0 1] (lazy-seq-fibo 0 1)))
    ([a b]
        (let [n (+ a b)]
            (lazy-seq (cons n (lazy-seq-fibo b n))))))

(def p5-applet
     (proxy [PApplet] []
            (setup []
                   (binding [*applet* this]
                     (size 800 600)
                     (smooth)
                     (no-stroke)
                     (fill 0)
                     (framerate 5)))
            (draw []
                  (binding [*applet* this]
                    (let [x (random (width))
                          y (random (width))]
                      (loop [rs (filter #(> % 0) (reverse (take 15 (lazy-seq-fibo))))]
                        (if (empty? rs)
                          nil
                          (do
                            (let [r (first rs) ]
                              (fill (random 200))
                              (ellipse x y r r))
                            (recur (rest rs))))))))))

(.init p5-applet)

(def swing-frame (JFrame. "Clojure Processing 1"))
(doto swing-frame
  ;;(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
  (.setDefaultCloseOperation WindowConstants/DISPOSE_ON_CLOSE)
  (.setSize 200 200)
  (.add p5-applet)
  (.pack)
  (.show))

(.stop p5-applet) としないと動き続けるので注意。