                     second where n is the target frame-rate set for
                     the visualisation.
 
+  :target         - one of :frame, :perm-frame. Default :frame.
+
   :focus-gained   - Called when the applet gains focus.
 
   :focus-lost     - Called when the applet loses focus.

  [applet]
  (.exit applet))

(defn applet-close
  "Stop the applet and close the window."
  [applet]
  (let [closing-fn (fn []
                     (let [frame @(:frame (meta applet))]
                       (.stop applet)
                       ;;.destroy appears to kill the process too
                       (doto frame
                         (.hide)
                         (.dispose))))]
    (javax.swing.SwingUtilities/invokeAndWait closing-fn))  )

(defn- applet-run
  "Launches the applet."
  ([applet] (applet-run applet nil false))
  ([applet mode keep-on-top?]
     (.init applet)
     (let [m (.meta applet)
           keep-on-top? (true? keep-on-top?)
           [width height & _] (or (:size m) [200 200])
           close-op (if (= :exit-on-close mode)
                      JFrame/EXIT_ON_CLOSE
                      JFrame/DISPOSE_ON_CLOSE)]
       (reset! (:frame m)
               (let [f (JFrame. (or (:title m) (str "Quil " (swap! untitled-applet-id* inc))))]
                 (doto f
                   (.addWindowListener  (reify WindowListener
                                          (windowActivated [this e])
                                          (windowClosing [this e]
                                            (future (applet-close applet)))
                                          (windowDeactivated [this e])
                                          (windowDeiconified [this e])
                                          (windowIconified [this e])
                                          (windowOpened [this e])
                                          (windowClosed [this e])))
                   (.setDefaultCloseOperation close-op)
                   (.setSize width height)
                   (.add applet)
                   (.pack))
                 (when (= mode :opengl)
                   (.setResizable f false))
                 (.show f)
                 (.setAlwaysOnTop f keep-on-top?)
                 f)))))

(def ^{:private true}
  renderer-modes {:p2d    P2D

                  :pdf    PDF
                  :dxf    DXF})

(defn- applet-set-size
  ([width height] (.size *applet* (int width) (int height)))
  ([width height renderer]
