Jim - FooBar(); | 9 Aug 2012 10:37
Picon

drawing a chess-board with seesaw ...

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

--

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure <at> googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
(Continue reading)

Stathis Sideris | 9 Aug 2012 11:08
Picon
Gravatar

Re: drawing a chess-board with seesaw ...

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

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...

Any seesaw gurus? -> please help...

cheers,
Jim

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure <at> 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 <at> googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
Jim - FooBar(); | 9 Aug 2012 12:17
Picon

Re: drawing a chess-board with seesaw ...

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)

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure <at> 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 <at> googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
Jim - FooBar(); | 9 Aug 2012 12:23
Picon

Re: drawing a chess-board with seesaw ...

On 09/08/12 11:17, Jim - FooBar(); wrote:
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)


aaa ok sorry...you mean having it as doseq binding...that makes sense! I apologise for rushing...

Jim

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure <at> 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 <at> googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
Jim - FooBar(); | 9 Aug 2012 12:53
Picon

Re: drawing a chess-board with seesaw ...

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

--

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

David Powell | 9 Aug 2012 13:00
Gravatar

Re: drawing a chess-board with seesaw ...

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

On Thu, Aug 9, 2012 at 11:53 AM, Jim - FooBar(); <jimpil1985 <at> gmail.com> wrote:
> 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
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure <at> 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 <at> googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--

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

Jim - FooBar(); | 9 Aug 2012 13:29
Picon

Re: drawing a chess-board with seesaw ...

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

--

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

Stathis Sideris | 9 Aug 2012 14:40
Picon
Gravatar

Re: drawing a chess-board with seesaw ...

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:
>
>    (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

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

Gmane