[racket] A beginner's question re drawing on a canvas

From: Sean Kanaley (skanaley at gmail.com)
Date: Wed Oct 1 22:01:48 EDT 2014

Hi,

Just make the drawing code part of on-paint. I have inlined a subclass for
you:

#lang racket/gui

(define frame (new frame%
                   [label "Example"]
                   [width 300]
                   [height 300]))
(define top-canvas (new (class canvas% (super-new [parent frame])
                          (define dc (send this get-dc))

                          (define/override (on-paint)
                            (send dc draw-rectangle
                                  0  10   ; Top-left at (0, 10), 10 pixels
down from top-left
                                  30 10) ; 30 pixels wide and 10 pixels high
                            (send dc draw-line
                                  0 0    ; Start at (0, 0), the top-left
corner
                                  30 30) ; and draw to (30, 30), the
bottom-right corner
                            (send dc draw-line
                                  0 30   ; Start at (0, 30), the
bottom-left corner
                                  30 0)  ; and draw to (30, 0), the
top-right corner
                            ))))

(send frame show #t)

But if you want, you can do something like

(define your-canvas
  (class canvas%
     ... ; as-above after the class canvas% part

Also you can combine commands to a single object with send*

(define/override (on-paint)
                            (send* dc
                              (draw-rectangle
                               0  10   ; Top-left at (0, 10), 10 pixels
down from top-left
                               30 10) ; 30 pixels wide and 10 pixels high
                              (draw-line
                               0 0    ; Start at (0, 0), the top-left corner
                               30 30) ; and draw to (30, 30), the
bottom-right corner
                              (draw-line
                               0 30   ; Start at (0, 30), the bottom-left
corner
                               30 0)  ; and draw to (30, 0), the top-right
corner
                              )))))


On Wed, Oct 1, 2014 at 9:51 PM, Chris Wright <cawright.99 at gmail.com> wrote:

> I would like to draw on a canvas in a window at various times during
> program execution.
> My first attempt was to combine two examples:
>
> #lang racket/gui
>
> (define frame (new frame%
>                    [label "Example"]
>                    [width 300]
>                    [height 300]))
> (define top-canvas (new canvas% [parent frame]))
>
> (send frame show #t)
>
> (define dc (send top-canvas get-dc))
>
> (send dc draw-rectangle
>       0  10   ; Top-left at (0, 10), 10 pixels down from top-left
>       30 10) ; 30 pixels wide and 10 pixels high
> (send dc draw-line
>       0 0    ; Start at (0, 0), the top-left corner
>       30 30) ; and draw to (30, 30), the bottom-right corner
> (send dc draw-line
>       0 30   ; Start at (0, 30), the bottom-left corner
>       30 0)  ; and draw to (30, 0), the top-right corner
>
>
>
> The cross and box are drawn, but "instantly" over-written by a blank
> canvas. I suppose this is because on-paint is triggered? (not sure by
> what..)
> If I put the (send frame...) form at the end of the code, the cross and
> box aren't seen.
>
> I am sure this is due to me not understanding the model properly - I'd be
> grateful for some help...
>
> many thanks
>
> Chris
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141001/23708310/attachment.html>

Posted on the users mailing list.