(ns rosado.processing.applet
  (:use [rosado.processing]
        [clojure.contrib.java-utils :only [as-str]])
  (:import (java.awt Frame)))

;; This is the definition of bind-applet that I need, but it causes
;; Caused by: java.lang.ClassNotFoundException: rosado.processing.applet$bind_applet__4814$fn__4816
;;  at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
;;  at java.security.AccessController.doPrivileged(Native Method)
;;  at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
;;  at clojure.lang.DynamicClassLoader.findClass(DynamicClassLoader.java:55)
;;  at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
;;  at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
;;  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
;;  at java.lang.Class.forName0(Native Method)
;;  at java.lang.Class.forName(Class.java:264)
;;  at clojure.lang.RT.classForName(RT.java:1487)
;;  at clojure.lang.LispReader$EvalReader.invoke(LispReader.java:928)
;;  at clojure.lang.LispReader$DispatchReader.invoke(LispReader.java:540)
;;  at clojure.lang.LispReader.read(LispReader.java:145)
;;  at clojure.lang.RT.readString(RT.java:1191)
;;  ... 37 more

(defn bind-applet [f]
  (fn [this & args]
    (binding [*applet* this] (apply f args))))

(defmacro defapplet
  "Define an applet. Takes an app-name and a map of options."
  [app-name & opts]
  (let [options (assoc (apply hash-map opts) :name (str app-name))
        fns (dissoc options :name :title :height :width)
        methods (zipmap (map name (keys fns))
                        (map bind-applet (vals fns)))]
    `(def ~app-name
          (let [frame# (atom nil)
                prx# (proxy [processing.core.PApplet
                             clojure.lang.IMeta] []
                       (meta [] (assoc ~options :frame frame#)))]
            (update-proxy prx# ~methods)

(defapplet ohai
  :setup (fn [] (framerate 1))
  :draw (fn [] (line 10 10 (frame-count) 100)))

;; (stop ohai)
;;  (run ohai)

Generated by Phil Hagelberg using scpaste at Wed Sep 2 20:33:23 2009. PDT. (raw)