annotate src/rlm/simple_drawing.clj @ 0:78a630e650d2

initial import
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Oct 2011 00:57:08 -0700
parents
children
rev   line source
rlm@0 1 (ns rlm.simple-drawing)
rlm@0 2
rlm@0 3
rlm@0 4
rlm@0 5 (def max-x 500)
rlm@0 6 (def max-y 500)
rlm@0 7 (def shapes (atom []))
rlm@0 8
rlm@0 9
rlm@0 10 (defn render-shape
rlm@0 11 [graphics [shape-name x y w h r g b a]]
rlm@0 12 (.setColor graphics (java.awt.Color. r g b a))
rlm@0 13 (case shape-name
rlm@0 14 rect (.fillRect graphics x y w h)
rlm@0 15 oval (.fillOval graphics x y w h)
rlm@0 16 line (.drawLine graphics x y w h)))
rlm@0 17
rlm@0 18 (def panel
rlm@0 19 (let [jp (proxy [javax.swing.JPanel]
rlm@0 20 []
rlm@0 21 (getPreferredSize [] (java.awt.Dimension. max-x max-y))
rlm@0 22 (paint [g]
rlm@0 23 (render-shape g ['rect 0 0 max-x max-y 255 255 255 255])
rlm@0 24 (doall (map #(render-shape g %) @shapes))))]
rlm@0 25 (doto (new javax.swing.JFrame "My graphics window")
rlm@0 26 (.setSize max-x max-y)
rlm@0 27 (.add jp)
rlm@0 28 (.setVisible true))
rlm@0 29 jp))
rlm@0 30
rlm@0 31
rlm@0 32 (defn draw-shape [& shape]
rlm@0 33 "Adds a shape to the current drawing. The first argument should be one of
rlm@0 34 the following symbols: rect, oval, or line. For rect or oval this should
rlm@0 35 be followed by: x, y, width, height, red, green, blue, alpha. The x and y
rlm@0 36 coordinates specify the upper right corner. Color values (including alpha,
rlm@0 37 which is opacity) should range from 0 to 255. For line the arguments are
rlm@0 38 the remaining arguments are x-start, y-start, x-end, y-end, red, green,
rlm@0 39 blue, alpha."
rlm@0 40 (swap! shapes conj shape)
rlm@0 41 (.paint panel (.getGraphics panel)))
rlm@0 42
rlm@0 43 (defn run []
rlm@0 44
rlm@0 45 (draw-shape 'rect 20 20 460 80 255 128 0 255)
rlm@0 46 (draw-shape 'oval 120 120 300 100 100 128 0 255)
rlm@0 47 (draw-shape 'line 0 0 500 500 255 0 0 255)
rlm@0 48 )