diff org/util.org @ 114:9d0fe7f54e14

merged image viewing code to cortex.util
author Robert McIntyre <rlm@mit.edu>
date Thu, 19 Jan 2012 22:19:24 -0700
parents 92b857b6145d
children b591da250afc
line wrap: on
line diff
     1.1 --- a/org/util.org	Thu Jan 19 22:08:46 2012 -0700
     1.2 +++ b/org/util.org	Thu Jan 19 22:19:24 2012 -0700
     1.3 @@ -94,19 +94,22 @@
     1.4    (:import com.jme3.scene.shape.Sphere)
     1.5    (:import com.jme3.light.AmbientLight)
     1.6    (:import com.jme3.light.DirectionalLight)
     1.7 -  (:import com.jme3.math.ColorRGBA)
     1.8 +  (:import (com.jme3.math Triangle ColorRGBA))
     1.9    (:import com.jme3.bullet.BulletAppState)
    1.10    (:import com.jme3.material.Material)
    1.11    (:import com.jme3.scene.Geometry)
    1.12 +  (:import java.awt.image.BufferedImage)
    1.13 +  (:import javax.swing.JPanel)
    1.14 +  (:import javax.swing.JFrame)
    1.15 +  (:import javax.swing.SwingUtilities)
    1.16 +  
    1.17    (:import (java.util.logging Level Logger)))
    1.18  
    1.19 -
    1.20 -
    1.21  (defvar println-repl
    1.22    (bound-fn [& args] (apply println args))
    1.23    "println called from the LWJGL thread will not go to the REPL, but
    1.24 -  instead to whatever terminal started the JVM process. This function
    1.25 -  will always output to the REPL")
    1.26 +   instead to whatever terminal started the JVM process. This function
    1.27 +   will always output to the REPL")
    1.28  
    1.29  (defn position-camera
    1.30    "Change the position of the in-world camera."
    1.31 @@ -388,6 +391,34 @@
    1.32  #+begin_src clojure :results silent
    1.33  (in-ns 'cortex.util)
    1.34  
    1.35 +(defn view-image
    1.36 +  "Initailizes a JPanel on which you may draw a BufferedImage.
    1.37 +   Returns a function that accepts a BufferedImage and draws it to the
    1.38 +   JPanel."
    1.39 +  []
    1.40 +  (let [image
    1.41 +        (atom
    1.42 +         (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
    1.43 +        panel 
    1.44 +        (proxy [JPanel] []
    1.45 +          (paint
    1.46 +            [graphics]
    1.47 +            (proxy-super paintComponent graphics)
    1.48 +            (.drawImage graphics @image 0 0 nil)))
    1.49 +        frame (JFrame. "Display Image")]
    1.50 +    (SwingUtilities/invokeLater
    1.51 +     (fn []
    1.52 +       (doto frame
    1.53 +         (-> (.getContentPane) (.add panel))
    1.54 +         (.pack)
    1.55 +         (.setLocationRelativeTo nil)
    1.56 +         (.setResizable true)
    1.57 +         (.setVisible true))))
    1.58 +    (fn [#^BufferedImage i]
    1.59 +      (reset! image i)
    1.60 +      (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
    1.61 +      (.repaint panel 0 0 (.getWidth i) (.getHeight i)))))
    1.62 +
    1.63  (defprotocol Viewable
    1.64    (view [something]))
    1.65