Mercurial > hg > indyvon
changeset 17:31382464ef27
Use Swing instead of plain AWT to get double buffering.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Thu, 17 Jun 2010 05:42:05 +0400 |
parents | 0fda22fc53d2 |
children | 740b9d2bbc45 |
files | src/indyvon/component.clj |
diffstat | 1 files changed, 32 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/src/indyvon/component.clj Thu Jun 17 05:20:37 2010 +0400 +++ b/src/indyvon/component.clj Thu Jun 17 05:42:05 2010 +0400 @@ -8,44 +8,47 @@ (:use indyvon.core indyvon.event) (:import (java.awt Component Dimension Color) - (java.awt.event MouseEvent))) - -(defn- make-update-fn [component] - (fn [] (.repaint component))) + (java.awt.event MouseEvent) + (javax.swing JFrame JPanel))) (defn- font-context [component] (.getFontRenderContext (.getFontMetrics component (.getFont component)))) -(defn make-component +(defn paint-component [component layer context graphics] + (let [size (.getSize component) + width (.width size) + height (.height size) + context (assoc context + :font-context (.getFontRenderContext graphics) + :update-fn #(.repaint component))] + (draw! context layer graphics 0 0 width height false)) + (commit (:dispatcher context))) + +(defn preferred-size [component layer context] + (let [context (assoc context + :font-context (font-context component)) + s (size layer context)] + (Dimension. (s 0) (s 1)))) + +(defn make-jpanel ([layer] - (make-component layer (make-event-dispatcher))) + (make-jpanel layer (make-event-dispatcher))) ([layer event-dispatcher] (let [context (default-context) - component - (proxy [Component] [] - (update [g] (.paint this g)) - (paint [g] - (let [size (.getSize this) - width (.width size) - height (.height size) - context (assoc context - :font-context (.getFontRenderContext g) - :dispatcher event-dispatcher - :update-fn (make-update-fn this))] - (draw! context layer g 0 0 width height false)) - (commit event-dispatcher)) + context (assoc context :dispatcher event-dispatcher) + panel + (proxy [JPanel] [] + (paintComponent [g] + (paint-component this layer context g)) (getPreferredSize [] - (let [context (assoc context - :font-context (font-context this)) - s (size layer context)] - (Dimension. (s 0) (s 1)))))] - (listen! event-dispatcher component) - (.setBackground component (-> context :theme :back-color)) - component))) + (preferred-size this layer context)))] + (listen! event-dispatcher panel) + (.setBackground panel (-> context :theme :back-color)) + panel))) (comment (do - (def frame (java.awt.Frame. "Test")) + (def frame (JFrame. "Test")) (def layer1 (reify Layer (render! [this context g] @@ -71,12 +74,12 @@ (.drawLine g 0 0 (:width context) (:height context)) (draw! context layer2 g 15 20) (draw! context layer3 g 100 100 80 50)) - (size [this context] [300 400]))) + (size [this context] [400 300]))) (doto frame (.addWindowListener (proxy [java.awt.event.WindowAdapter] [] (windowClosing [event] (.dispose frame)))) - (.add (make-component layer)) + (.. (getContentPane) (add (make-jpanel layer))) (.pack) (.setVisible true))