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

From: 김태윤 (kty1104 at gmail.com)
Date: Wed Oct 27 02:57:33 EDT 2010

thank you very much
the following code works well
===================================================
#lang scheme/gui
(require 2htdp/image)
(define pic dc<%>)
(define chipset (make-object bitmap% "town.png" 'png))
(define f (instantiate frame% ("map editor")))
(define mcan%
  (class canvas%

    (override on-event on-paint)
    (define on-paint
      (λ () (send (send this get-dc) draw-bitmap chipset 0 0)))
    (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 (image-width chipset))
(min-height (image-height chipset))))

(define msg (instantiate message% ("nothing so far" f)))
(send f show #t)
===================================================

2010/10/27 Ryan Culpepper <ryanc at ccs.neu.edu>

> 김태윤 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101027/3850b769/attachment.html>

Posted on the users mailing list.