[racket] drawing on canvas what's wrong with my code?

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Wed Oct 27 01:52:38 EDT 2010

김태윤 wrote:
> drawing on canvas what's wrong with my code?
> 
> I coded as follow and run it but nothing apear.
> could sombody tell me what's wrong with my code?
> 
> #lang scheme/gui
> (require 2htdp/image)
> (define pic dc<%>)
> 
> (define f (instantiate frame% ("map editor")))
> (define mcan%
>   (class canvas%
> 
>     (override on-event)
> 
>     (define on-event (λ (e)
>                        (send msg set-label
>                              (string-append (number->string (send e get-x)) " "
>                                             (number->string (send e get-y))))))
>     (super-instantiate ())))
> 
> (define mcan (new mcan% (parent f) (min-width 400) (min-height 400)))
> (send (send mcan get-dc) draw-bitmap (make-object bitmap% "town.png"
> 'png) 16 16)
> 
> (define msg (instantiate message% ("nothing so far" f)))
> (send f show #t)

A plain canvas% doesn't remember what's drawn to it. In your code, 
you're drawing to the canvas before the frame is visible, and by the 
time the canvas is shown it has forgotten what you've drawn on it.

The code that draws on the canvas's dc should be inside the canvas's 
on-paint method:

   (class canvas%
     ...
     (define/override (on-paint)
       (send (send this get-canvas) draw-bitmap the-bitmap 16 16)
       (super on-paint))
     ...)

Ryan


Posted on the users mailing list.