Mercurial > rlm
view 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 source
1 (ns rlm.simple-drawing)5 (def max-x 500)6 (def max-y 500)7 (def shapes (atom []))10 (defn render-shape11 [graphics [shape-name x y w h r g b a]]12 (.setColor graphics (java.awt.Color. r g b a))13 (case shape-name14 rect (.fillRect graphics x y w h)15 oval (.fillOval graphics x y w h)16 line (.drawLine graphics x y w h)))18 (def panel19 (let [jp (proxy [javax.swing.JPanel]20 []21 (getPreferredSize [] (java.awt.Dimension. max-x max-y))22 (paint [g]23 (render-shape g ['rect 0 0 max-x max-y 255 255 255 255])24 (doall (map #(render-shape g %) @shapes))))]25 (doto (new javax.swing.JFrame "My graphics window")26 (.setSize max-x max-y)27 (.add jp)28 (.setVisible true))29 jp))32 (defn draw-shape [& shape]33 "Adds a shape to the current drawing. The first argument should be one of34 the following symbols: rect, oval, or line. For rect or oval this should35 be followed by: x, y, width, height, red, green, blue, alpha. The x and y36 coordinates specify the upper right corner. Color values (including alpha,37 which is opacity) should range from 0 to 255. For line the arguments are38 the remaining arguments are x-start, y-start, x-end, y-end, red, green,39 blue, alpha."40 (swap! shapes conj shape)41 (.paint panel (.getGraphics panel)))43 (defn run []45 (draw-shape 'rect 20 20 460 80 255 128 0 255)46 (draw-shape 'oval 120 120 300 100 100 128 0 255)47 (draw-shape 'line 0 0 500 500 255 0 0 255)48 )