rlm@0: (ns rlm.simple-drawing) rlm@0: rlm@0: rlm@0: rlm@0: (def max-x 500) rlm@0: (def max-y 500) rlm@0: (def shapes (atom [])) rlm@0: rlm@0: rlm@0: (defn render-shape rlm@0: [graphics [shape-name x y w h r g b a]] rlm@0: (.setColor graphics (java.awt.Color. r g b a)) rlm@0: (case shape-name rlm@0: rect (.fillRect graphics x y w h) rlm@0: oval (.fillOval graphics x y w h) rlm@0: line (.drawLine graphics x y w h))) rlm@0: rlm@0: (def panel rlm@0: (let [jp (proxy [javax.swing.JPanel] rlm@0: [] rlm@0: (getPreferredSize [] (java.awt.Dimension. max-x max-y)) rlm@0: (paint [g] rlm@0: (render-shape g ['rect 0 0 max-x max-y 255 255 255 255]) rlm@0: (doall (map #(render-shape g %) @shapes))))] rlm@0: (doto (new javax.swing.JFrame "My graphics window") rlm@0: (.setSize max-x max-y) rlm@0: (.add jp) rlm@0: (.setVisible true)) rlm@0: jp)) rlm@0: rlm@0: rlm@0: (defn draw-shape [& shape] rlm@0: "Adds a shape to the current drawing. The first argument should be one of rlm@0: the following symbols: rect, oval, or line. For rect or oval this should rlm@0: be followed by: x, y, width, height, red, green, blue, alpha. The x and y rlm@0: coordinates specify the upper right corner. Color values (including alpha, rlm@0: which is opacity) should range from 0 to 255. For line the arguments are rlm@0: the remaining arguments are x-start, y-start, x-end, y-end, red, green, rlm@0: blue, alpha." rlm@0: (swap! shapes conj shape) rlm@0: (.paint panel (.getGraphics panel))) rlm@0: rlm@0: (defn run [] rlm@0: rlm@0: (draw-shape 'rect 20 20 460 80 255 128 0 255) rlm@0: (draw-shape 'oval 120 120 300 100 100 128 0 255) rlm@0: (draw-shape 'line 0 0 500 500 255 0 0 255) rlm@0: )