[racket] Newbie question: creating a custom canvas

From: Gregory Woodhouse (gregwoodhouse at me.com)
Date: Thu Aug 23 20:29:40 EDT 2012

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.

Posted on the users mailing list.