   [applet]
   (.exit applet))
 
-(defn applet-close
-  "Stop the applet and close the window."
+(defn- applet-close-applet
+  [applet]
+  (.stop applet))
+
+(defn- applet-close-frame
   [applet]
   (let [closing-fn (fn []
-                     (let [frame @(:frame (meta applet))]
-                       (.stop applet)
+                     (let [frame @(:target-obj (meta applet))]
+                       (applet-close-applet applet)
                        ;;.destroy appears to kill the process too
                        (doto frame
                          (.hide)
                          (.dispose))))]
-    (javax.swing.SwingUtilities/invokeAndWait closing-fn))  )
+    (javax.swing.SwingUtilities/invokeAndWait closing-fn)))
+
+(defn applet-close
+  "Stop the applet and close the window."
+  [applet]
+  (case (:target (meta applet))
+    :frame      (applet-close-frame applet)
+    :perm-frame (applet-close-frame applet)
+    :applet     (applet-close-applet applet)))
+
+(defn- launch-applet-frame
+  [applet renderer keep-on-top?]
+  (let [truthify       #(not (or (nil? %) (false? %)))
+        keep-on-top?   (truthify keep-on-top?)
+        m              (.meta applet)
+        [width height] (or (:size m) [800 600])
+        close-op       JFrame/DISPOSE_ON_CLOSE
+        title          (or (:title m) (str "Quil " (swap! untitled-applet-id* inc)))
+        f              (JFrame. title)]
+    (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 (= renderer :opengl)
+      (.setResizable f false))
+    (.show f)
+    (.setAlwaysOnTop f keep-on-top?)
+    (reset! (:target-obj (meta applet)) f)
+    applet))
 
 (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)))))
+  "Launches the applet to the specified target."
+  [applet renderer target]
+  (.init applet)
+  (case target
+    :frame (launch-applet-frame applet renderer false)
+    :perm-frame (launch-applet-frame applet renderer true)
+    :applet applet))
 
 (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]

                    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.
