view src/net/kryshen/indyvon/component.clj @ 119:91c341698f7e

Use FontRenderContext based on rendering hints when Graphics is not available. Obtain text rendering hints from desktop properties.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Apr 2012 18:28:12 +0400
parents 2bd613007e97
children 24c7935a8f06
line source
1 ;;
2 ;; Copyright 2010, 2011 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 [size (.getSize c)]
31 (.setColor g (:back-color *theme*))
32 (.fillRect g 0 0 (.width size) (.height size))
33 (draw-scene! scene g (.width size) (.height size))))
35 (defn- preferred-size [^Component c scene]
36 (let [geom (scene-geometry scene)]
37 (Dimension. (width geom) (height geom))))
39 (defn ^JPanel make-jpanel
40 ([layer]
41 (make-jpanel layer (root-event-dispatcher)))
42 ([layer event-dispatcher]
43 (let [panel (proxy [JPanel] [])
44 scene (make-scene
45 layer event-dispatcher panel
46 (.getDesktopProperty (java.awt.Toolkit/getDefaultToolkit)
47 "awt.font.desktophints"))]
48 (update-proxy
49 panel
50 {"paintComponent" #(paint-component %1 %2 scene)
51 "getPreferredSize" #(preferred-size % scene)})
52 (.setBackground panel (:back-color *theme*))
53 (add-observer panel scene (fn [w _]
54 ;; Use the first observer argument
55 ;; instead of closing over panel to
56 ;; allow the panel and associated
57 ;; observer to be gc'd.
58 (.repaint ^Component w)))
59 (listen! event-dispatcher panel)
60 panel)))
62 (defn make-jframe [^String title layer]
63 (doto (JFrame. title)
64 (.. (getContentPane) (add (make-jpanel layer)))
65 (.pack)))
67 (defn message [m]
68 (JOptionPane/showMessageDialog (:component *scene*) m))