[racket] Newbie question: creating a custom canvas

From: Richard Cleis (rcleis at me.com)
Date: Thu Aug 23 22:55:45 EDT 2012

I dont know enough about this stuff, other than to 'rather slavishly' follow your code and make it work. :O)

Try this:

...
   (define/override (on-paint) ; eliminate the argument
     (define dc (send this get-dc)) ; 'this' is the current instance
...

On Aug 23, 2012, at 6:29 PM, Gregory Woodhouse wrote:

> I apologize in advance for the elementary nature of this question, and for the amount of code included below. I'm having a tough time following the documentation for racket/gui and racket/draw. My first attempt to create a Sudoku grid follows the example in the documentation rather slavishly and works fine
> 
> 
> (define frame (new frame% [label "Sudoku"]
>                   [width 300]
>                   [height 300]))
> 
> (define grid-canvas (new canvas% [parent frame]
>                                 [min-width 300]
>                                 [min-height 300]
>                                 [paint-callback
>                 (lambda (canvas dc)
>                   (let
>                       ([w (send canvas get-width)]
>                        [h (send canvas get-height)])
>                     (send dc set-pen "black" 3 'solid)
>                     (for ([i (in-range 3)])
>                       (send dc draw-line 0 (* i (/ h 3)) w (* i (/ h 3)))
>                       (send dc draw-line (* i (/ w 3)) 0 (* i (/ w 3)) h))
>                     (send dc set-pen "black" 1 'solid)
>                     (for ([i (in-range 9)])
>                           (send dc draw-line 0 (* i (/ h 9)) w (* i (/ h 9))))
>                     (for ([i (in-range 9)])
>                       (send dc draw-line (* i (/ w 9)) 0 (* i (/ w 9)) h))))]))
> 
> ; Show the frame by calling its show method
> (send frame show #t)
> 
> 
> But this does not work
> 
> (define frame (new frame% [label "Sudoku"]
>                   [width 300]
>                   [height 300]))
> 
> (define grid-canvas%
>  (class canvas%
>    (define/override (on-paint dc)
>      (let
>          ([w (send this get-width)]
>           [h (send this get-height)])
>        (send dc set-pen "black" 3 'solid)
>                     (for ([i (in-range 3)])
>                       (send dc draw-line 0 (* i (/ h 3)) w (* i (/ h 3)))
>                       (send dc draw-line (* i (/ w 3)) 0 (* i (/ w 3)) h))
>                     (send dc set-pen "black" 1 'solid)
>                     (for ([i (in-range 9)])
>                           (send dc draw-line 0 (* i (/ h 9)) w (* i (/ h 9))))
>                     (for ([i (in-range 9)])
>                       (send dc draw-line (* i (/ w 9)) 0 (* i (/ w 9)) h))))
>    (super-new)))
> 
> (define the-grid (new grid-canvas% [parent frame]
>                                   [min-width 300]
>                                   [min-height 300]))
> 
> 
> I get an arity mismatch in on-paint.
> 
> The reason for not wanting in to pass in the paint-callback is that I want to be able to augment it somewhat in grid-canvas% (specifically, I need to draw in the digits based on the contents of the grid!) It's not clear (to me) how to do this, or if I should be taking an entirely different approach.
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users


Posted on the users mailing list.