       .show))
   reference)
 
+(defn- default-screen-device []
+  (->
+    (java.awt.GraphicsEnvironment/getLocalGraphicsEnvironment)
+    .getDefaultScreenDevice))
+
+(defn bind-key
+  "Maps an input-key on a swing component to an action,
+  such that action-fn is executed when key is pressed."
+  [component input-key action-fn global?]
+  (let [im (.getInputMap component (if global?
+                                     JComponent/WHEN_IN_FOCUSED_WINDOW
+                                     JComponent/WHEN_FOCUSED))
+        am (.getActionMap component)
+        input-event (KeyStroke/getKeyStroke input-key)
+        action
+          (proxy [AbstractAction] []
+            (actionPerformed [e]
+                (action-fn)))
+        uuid (.. UUID randomUUID toString)]
+    (.put im input-event uuid)
+    (.put am uuid action)))
+
+(defn bind-window-key
+  [window input-key action-fn]
+  (bind-key (.getContentPane window) input-key action-fn true))
+
+(defn full-screen! ; adapted from seesaw
+  "Make the given window/frame full-screen. Pass nil to return all windows
+to normal size."
+  ([^java.awt.GraphicsDevice device window]
+    (if window
+      (do
+        (.dispose window)
+        (.setUndecorated window true)
+        (.setFullScreenWindow device window)
+        (.show window)
+        )
+      (let [window (.getFullScreenWindow device)]
+        (.setFullScreenWindow device nil)
+        (when window
+          (.dispose window)
+          (.setUndecorated window false)
+          (.show window))))
+    window)
+  ([window]
+    (full-screen! (default-screen-device) window)))
+
+(defn setup-fullscreen [window]
+  (bind-window-key window "F" #(full-screen! window))
+  (bind-window-key window "ESCAPE" #(full-screen! nil)))
+
 (defrecord screen-properties [x y z width height zoom])  
 
 (def screen-state (atom nil))

(ns slide_explorer.display
  (:import (javax.swing JFrame JLabel)
           (java.awt Canvas Graphics Graphics2D RenderingHints)
           (java.awt.event MouseAdapter WindowAdapter)
           (java.awt.image BufferedImage WritableRaster)
           (java.awt.geom AffineTransform)
           (java.io ByteArrayInputStream)
           (javax.imageio ImageIO)
           (mmcorej TaggedImage)
           (org.micromanager.utils GUIUpdater ImageUtils MDUtils))

    (let [graphics2d (cast Graphics2D g)]
      (.setRenderingHint graphics2d
                         RenderingHints/KEY_ANTIALIASING
                         (if on
                           RenderingHints/VALUE_ANTIALIAS_ON
                           RenderingHints/VALUE_ANTIALIAS_OFF)))))
       
(defn draw-tile [^Graphics2D g image-data]
  (.drawImage g (buffered-image [512 512] (:pixels image-data))
              (AffineTransform.) nil))

(defn paint-tiles [canvas screen-state]
  (let [g (.getGraphics canvas)]
    (doto g
      enable-anti-aliasing
      (.setColor Color/YELLOW)
      (.fillOval (- (+ (/ (:width @screen-state) 2)
                       (:x @screen-state))
                    30)
                 (- (+ (/ (:height @screen-state) 2)
                       (:y @screen-state))
                    30)
                 60 60)
      (.setColor Color/GRAY)
      (.drawString (str @screen-state)
                   (int 0)
                   (int (- (:height @screen-state) 10))))))
  
(defn paint-screen [canvas screen-state]
  (paint-tiles canvas screen-state))

(defn handle-drags [component position-atom]
  (let [drag-origin (atom nil)
