Mercurial > rlm
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78a630e650d2 |
---|---|
1 (ns rlm.simple-drawing) | |
2 | |
3 | |
4 | |
5 (def max-x 500) | |
6 (def max-y 500) | |
7 (def shapes (atom [])) | |
8 | |
9 | |
10 (defn render-shape | |
11 [graphics [shape-name x y w h r g b a]] | |
12 (.setColor graphics (java.awt.Color. r g b a)) | |
13 (case shape-name | |
14 rect (.fillRect graphics x y w h) | |
15 oval (.fillOval graphics x y w h) | |
16 line (.drawLine graphics x y w h))) | |
17 | |
18 (def panel | |
19 (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)) | |
30 | |
31 | |
32 (defn draw-shape [& shape] | |
33 "Adds a shape to the current drawing. The first argument should be one of | |
34 the following symbols: rect, oval, or line. For rect or oval this should | |
35 be followed by: x, y, width, height, red, green, blue, alpha. The x and y | |
36 coordinates specify the upper right corner. Color values (including alpha, | |
37 which is opacity) should range from 0 to 255. For line the arguments are | |
38 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))) | |
42 | |
43 (defn run [] | |
44 | |
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 ) |