drawing a chess-board with seesaw ...

310 views
Skip to first unread message

Jim - FooBar();

unread,
Aug 9, 2012, 4:37:48 AM8/9/12
to clo...@googlegroups.com
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...

Any seesaw gurus? -> please help...

cheers,
Jim

Stathis Sideris

unread,
Aug 9, 2012, 5:08:42 AM8/9/12
to clo...@googlegroups.com
How about drawing all the rectangles with .fillRect() and before each call alternate between black and white by calling .setColor(). You can alternate between the colours by taking the first of the following seq every time you set the color:

(cycle [java.awt.Color.black java.awt.Color.white])

Stathis

Jim - FooBar();

unread,
Aug 9, 2012, 6:17:01 AM8/9/12
to clo...@googlegroups.com
On 09/08/12 10:08, Stathis Sideris wrote:
How about drawing all the rectangles with .fillRect() and before each call alternate between black and white by calling .setColor(). You can alternate between the colours by taking the first of the following seq every time you set the color:

(cycle [java.awt.Color.black java.awt.Color.white])

Stathis

Geia sou Stathi,

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)

Jim - FooBar();

unread,
Aug 9, 2012, 6:23:41 AM8/9/12
to clo...@googlegroups.com
aaa ok sorry...you mean having it as doseq binding...that makes sense! I apologise for rushing...

Jim

Jim - FooBar();

unread,
Aug 9, 2012, 6:53:52 AM8/9/12
to clo...@googlegroups.com
On 09/08/12 11:23, Jim - FooBar(); wrote:
> 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

David Powell

unread,
Aug 9, 2012, 7:00:23 AM8/9/12
to clo...@googlegroups.com
You can try using the multi-input version of map to knit your data
together with some other, potentially infinite, sequence:

(map vector items (cycle [black white]))

It returns something like this:

([item1 black] [item2 white] [item3 black] [item4 white])

Then you can use doseq over that, using destructuring to pick apart
the items and colours and do something appropriate with each of them.

--
Dave
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Jim - FooBar();

unread,
Aug 9, 2012, 7:29:12 AM8/9/12
to clo...@googlegroups.com
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:
>
> (map vector items (cycle [black white]))
>
> It returns something like this:
>
> ([item1 black] [item2 white] [item3 black] [item4 white])
>
> 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!!!

Jim

Stathis Sideris

unread,
Aug 9, 2012, 8:40:29 AM8/9/12
to clo...@googlegroups.com
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!
Reply all
Reply to author
Forward
0 new messages