view src/net/kryshen/indyvon/component.clj @ 148:613bd4ac1bc0

Panel has background color argument.
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 25 Apr 2013 04:01:33 +0400
parents d37d6c5b3b08
children cb108c6fa079
line source
1 ;;
2 ;; Copyright 2010, 2011, 2012 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
6 ;; Indyvon is free software: you can redistribute it and/or modify it
7 ;; under the terms of the GNU Lesser General Public License version 3
8 ;; only, as published by the Free Software Foundation.
9 ;;
10 ;; Indyvon is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with Indyvon. If not, see
17 ;; <http://www.gnu.org/licenses/>.
18 ;;
20 (ns net.kryshen.indyvon.component
21 "Integrating Indyvon into AWT and Swing components."
22 (:use
23 net.kryshen.indyvon.core)
24 (:import
25 (java.awt Graphics Component Dimension Color)
26 (java.awt.geom Rectangle2D$Double)
27 (javax.swing JFrame JPanel JOptionPane)))
29 (defn- paint-component [^Component c ^Graphics g scene]
30 (let [w (.getWidth c)
31 h (.getHeight c)]
32 (.setColor g (:back-color *theme*))
33 (.fillRect g 0 0 w h)
34 (draw-scene! scene g w h)))
36 (defn- preferred-size [^Component c scene]
37 (let [geom (scene-geometry scene)]
38 (Dimension. (width geom) (height geom))))
40 (defn ^JPanel make-jpanel
41 ([layer]
42 (make-jpanel layer (root-event-dispatcher)))
43 ([layer event-dispatcher]
44 (let [panel (proxy [JPanel] [])
45 scene (make-scene
46 layer event-dispatcher panel
47 (.getDesktopProperty (java.awt.Toolkit/getDefaultToolkit)
48 "awt.font.desktophints"))]
49 (update-proxy
50 panel
51 {"paintComponent" #(paint-component %1 %2 scene)
52 "getPreferredSize" #(preferred-size % scene)})
53 (.setBackground panel (:back-color *theme*))
54 (add-observer panel scene (fn [w _]
55 ;; Use the first observer argument
56 ;; instead of closing over panel to
57 ;; allow the panel and associated
58 ;; observer to be gc'd.
59 (.repaint ^Component w)))
60 (listen! event-dispatcher panel)
61 panel)))
63 (defn ^JFrame make-jframe [^String title layer]
64 (doto (JFrame. title)
65 (.. (getContentPane) (add (make-jpanel layer)))
66 (.pack)))
68 (defn message [m]
69 (JOptionPane/showMessageDialog (:component *scene*) m))