Spent most of yesterday trying to draw a chessboard on a paintable canvas, however I'm stuck after drawing the lines of the grid...I mean the grid is there but its all one colour (the background colour of the panel)! The fn that draws the lines is simply this:
(defn draw-grid [c g]
(let [w (width c)
h (height c)]
(doseq [x (range 0 w 50)]
(.drawLine g x 0 x h))
(doseq [y (range 0 h 50)]
(.drawLine g 0 y w y))))
Does anyone have any suggestions as to how to do the black-white alteration on the grid? I know how to do it imperatively using loop(s) and a couple of flags but I'm really struggling to tweak the 'draw-grid' accordingly to paint the colours as well...
On Thursday, 9 August 2012 09:37:48 UTC+1, Jim foo.bar wrote:
> Hey all,
> Spent most of yesterday trying to draw a chessboard on a paintable > canvas, however I'm stuck after drawing the lines of the grid...I mean > the grid is there but its all one colour (the background colour of the > panel)! The fn that draws the lines is simply this:
> (defn draw-grid [c g] > (let [w (width c) > h (height c)] > (doseq [x (range 0 w 50)] > (.drawLine g x 0 x h)) > (doseq [y (range 0 h 50)] > (.drawLine g 0 y w y))))
> Does anyone have any suggestions as to how to do the black-white > alteration on the grid? I know how to do it imperatively using loop(s) > and a couple of flags but I'm really struggling to tweak the 'draw-grid' > accordingly to paint the colours as well...
If I understood correctly, you propose filling rectangles instead of drawing lines...that is a good idea but how would I alternate the colors using 'cycle'? 'cycle' returns a infinitely cycled lazy-seq of the provided collection. Whenever i call 'first' on it I get the same value back...they are not alternating! Have I misunderstood? thanks for bothering btw... :-)
> If I understood correctly, you propose filling rectangles instead of > drawing lines...that is a good idea but how would I alternate the > colors using 'cycle'? 'cycle' returns a infinitely cycled lazy-seq of > the provided collection. Whenever i call 'first' on it I get the same > value back...they are not alternating! Have I misunderstood? thanks > for bothering btw... :-)
> Jim (Dimitris)
aaa ok sorry...you mean having it as doseq binding...that makes sense! I apologise for rushing...
>> aaa ok sorry...you mean having it as doseq binding...that makes sense! I
>> apologise for rushing...
>> Jim
> No I can't put 'cycle' inside a doseq cos its trying to consume it!
> Jim
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> Then you can use doseq over that, using destructuring to pick apart
> the items and colours and do something appropriate with each of them.
Thanks Dave that is pretty clever and looks very idiomatic! I managed to get what I by using your suggestion:
(defn draw-grid2 [d g]
(let [w (width d)
h (height d)
tiles (map vector (for [x (range 0 w 50)
y (range 0 h 50)] [x y])
(cycle [java.awt.Color/WHITE
java.awt.Color/BLACK]))]
(doseq [[[x y] c] tiles]
(.setColor g c)
(.fillRect g x y 50 50)) ))
Thanks a lot! It looked impossible to achieve without mutation, indices and counting pixels!!!
Yeah, sorry Dimitri, I wasn't very clear :-) I meant that if you were going to do it recursively you would be using the first element of the seq, and you would be passing the (rest) of the seq to the subsequent recursive call. Very elegant solution!
On Thursday, 9 August 2012 12:29:12 UTC+1, Jim foo.bar wrote:
> On 09/08/12 12:00, David Powell wrote: > > You can try using the multi-input version of map to knit your data > > together with some other, potentially infinite, sequence: