Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding fields to a proxied class in Clojure

I'm using "proxy" to extend various Swing classes in a Clojure GUI application, generally with code that looks something like:

(def ^JPanel mypanel 
  (proxy [JPanel] []
    (paintComponent [#^Graphics g]
      (.drawImage g background-image 0 0 nil))))

This works well but I can't figure out how to add additional fields to the newly extended class, for example making the background-image a field that could be subsequently updated. This would be pretty easy and common practice in Java.

Is there a good way to do this in Clojure? Or is there another preferred method to achieve the same effect?

like image 503
mikera Avatar asked Jun 16 '10 20:06

mikera


1 Answers

You can use something like this:

(defn ^JPanel mypanel [state]
  (proxy [JPanel] []
    (paintComponent [#^Graphics g]
      (do
        (comment do something with state)
        (.drawImage g background-image 0 0 nil)))))

or use any other outer function/ref.

like image 99
Igor Artamonov Avatar answered Sep 23 '22 01:09

Igor Artamonov