diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/rlm/simple_drawing.clj	Tue Oct 18 00:57:08 2011 -0700
     1.3 @@ -0,0 +1,48 @@
     1.4 +(ns rlm.simple-drawing)
     1.5 +
     1.6 +
     1.7 +
     1.8 +(def max-x 500)
     1.9 +(def max-y 500)
    1.10 +(def shapes (atom []))
    1.11 +
    1.12 +
    1.13 +(defn render-shape
    1.14 +  [graphics [shape-name x y w h r g b a]]
    1.15 +  (.setColor graphics (java.awt.Color. r g b a))
    1.16 +  (case shape-name
    1.17 +	rect (.fillRect graphics x y w h)
    1.18 +	oval (.fillOval graphics x y w h)
    1.19 +	line (.drawLine graphics x y w h)))
    1.20 +
    1.21 +(def panel
    1.22 +     (let [jp (proxy [javax.swing.JPanel]
    1.23 +		  []
    1.24 +		(getPreferredSize [] (java.awt.Dimension. max-x max-y))
    1.25 +		(paint [g]
    1.26 +		       (render-shape g ['rect 0 0 max-x max-y 255 255 255 255])
    1.27 +		       (doall (map #(render-shape g %) @shapes))))]
    1.28 +       (doto (new javax.swing.JFrame "My graphics window")
    1.29 +	 (.setSize max-x max-y)
    1.30 +	 (.add jp)
    1.31 +	 (.setVisible true))
    1.32 +       jp))
    1.33 +
    1.34 +
    1.35 +(defn draw-shape [& shape]
    1.36 +  "Adds a shape to the current drawing. The first argument should be one of
    1.37 +the following symbols: rect, oval, or line. For rect or oval  this should
    1.38 +be followed by: x, y, width, height, red, green, blue, alpha. The x and y
    1.39 +coordinates specify the upper right corner. Color values (including alpha,
    1.40 +which is opacity) should range from 0 to 255. For line the arguments are
    1.41 +the remaining arguments are x-start, y-start, x-end, y-end, red, green,
    1.42 +blue, alpha."
    1.43 +  (swap! shapes conj shape)
    1.44 +  (.paint panel (.getGraphics panel)))
    1.45 +
    1.46 +(defn run []
    1.47 +  
    1.48 +  (draw-shape 'rect 20 20 460 80 255 128 0 255)
    1.49 +  (draw-shape 'oval 120 120 300 100 100 128 0 255)
    1.50 +  (draw-shape 'line 0 0 500 500 255 0 0 255)
    1.51 +)