1. On my Mac, I get the canvas, I see the circle, it sleeps for a second, the 
circle goes away --- precisely because paint-callback is called and it draws 
emptiness. 

2. If you don't see this, I think you're encountering a bug. This behavior 
should be the same across platforms. Report details. 

3. If you are a beginner, you may wish to study How to Design Programs 2e 
first: 

http://www.ccs.neu.edu/home/matthias/HtDP2e/Draft/index.html

4. Here is a stupid annotation with on-paint: 

#lang racket

(require slideshow racket/class racket/gui/base)

(define my-frame
  (new frame%
       [label "my frame"]
       [width 300] [height 300]
       [alignment '(center center)]))

(define x 40)
(define y 40)

(define my-canvas
  (new (class canvas%
         (super-new [parent my-frame])
         [define/override on-paint
           (lambda ()
             (displayln `(on-paint ,x ,y))
             (define my-dc (send my-canvas get-dc))
             (draw-pict (circle 60) my-dc x y)
             (set! x (+ x 3))
             (set! y (+ (random 3) y)))])))

(send my-frame show #t)

(for ((i (in-range 100)))
  (send my-canvas on-paint))

5. Next read up on timers. 




On Jun 25, 2015, at 7:26 AM, Mianlai ZHOU <[email protected]> 
wrote:

> Hello Matthias,
> 
> Thank you very much for what you have answered for my question. I have 
> copy-pasted and run the program
> you gave below, but the outcome is a bit unexpected. Due to the fact that 
> there is a line
> 
> (draw-pict (circle 60) my-dc 40 40),
> 
> I thought it would draw a circle with radius 60 at point (40, 40), and then 
> wait for 1 minute. But actually, it
> draws nothing, on the canvas it is just blank, and then (after 1 min) it 
> turns to another background color.
> It is not expected.
> 
> What I really wanted to do is, to make animation, i.e., to draw some content 
> on canvas, and then (after a
> certain period of time or upon the event such as a mouse button click) 
> replace the content with other pictures
> on the same canvas. My question is now, how can I do it?
> 
> It seems to me obviously, without paint-callback I would be unable to do it. 
> Is it so? But with paint-callback I
> can only draw one picture, how can I update it? How to make animation?
> 
> Thank you for your help again and I hope you would be so kind to give me more 
> help on this question.
> 
> Thanks,
> 
> A Chinese beginner computer scientist, Mianlai
> 
> 
> ________________________________________
> From: Matthias Felleisen [[email protected]]
> Sent: 23 June 2015 6:07
> To: Mianlai Zhou
> Cc: Racket-Users List
> Subject: Re: [racket-users] drawing with canvas
> 
> [[ I am tempted to say that you misplaced the parentheses and brackets and 
> broke lines at the wrong place. ]]
> 
> Here is a re-ordering of the first "don't work" variant that kind of works:
> 
> #lang racket
> 
> (require slideshow racket/class racket/gui/base)
> 
> (define my-frame
>  (new frame%
>       [label "my frame"]
>       [width 300] [height 300]
>       [alignment '(center center)]))
> 
> (define my-canvas (new canvas% [parent my-frame]))
> (define my-dc (send my-canvas get-dc))
> (send my-frame show #t)
> (draw-pict (circle 60) my-dc 40 40)
> (sleep 1)
> 
> Stop! This should give you a hint of why you need to have an callback 
> function in this world.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> In this world, many different events affect the visible canvas and it needs 
> to refresh itself all the time. Omitting the callback says "draw nothing" 
> when you refresh.
> 
> 
> 
> 
> On Jun 22, 2015, at 2:17 PM, Mianlai Zhou <[email protected]> wrote:
> 
>> Hi people,
>> 
>> I am wondering why this segment of code failed to work:
>> 
>> ; don't work
>> #lang racket
>> 
>> (require slideshow racket/class racket/gui/base)
>> 
>> (define my-frame (new frame% [label "my frame"]
>>                             [width 300] [height 300]
>>                             [alignment '(center center)] ))
>> 
>> (define my-canvas
>>      (new canvas% [parent my-frame]
>>          ))
>> 
>> (define my-dc (send my-canvas get-dc))
>> 
>> (draw-pict (circle 60) my-dc 40 40)
>> 
>> (send my-frame show #t)
>> 
>> While the following codes *do* work:
>> 
>> ; do work
>> #lang racket
>> 
>> (require slideshow racket/class racket/gui/base)
>> 
>> (define my-frame (new frame% [label "my frame"]
>>                             [width 300] [height 300]
>>                             [alignment '(center center)] ))
>> 
>> (define my-canvas
>>      (new canvas% [parent my-frame]
>>                   [paint-callback (lambda (self dc)
>>                                  (draw-pict (circle 60) dc 40 40)
>>                               )]
>>          ))
>> 
>> (send my-frame show #t)
>> 
>> Is there anyway to avoid using redefining paint-callback to draw one or more
>> pictures on the canvas, as in the first example?
>> 
>> Thanks in advance for your answer,
>> 
>> Mianlai
>> 
>> 
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
> 
> 
> This message and any attachment are intended solely for the addressee and may 
> contain confidential information.
> If you have received this message in error, please send it back to me, and 
> immediately delete it.
> 
> Please do not use, copy or disclose the information contained in this message 
> or in any attachment.
> 
> Any views or opinions expressed by the author of this email do not 
> necessarily reflect the views of The University of Nottingham Ningbo China.
> 
> 
> This message has been checked for viruses but the contents of an attachment 
> may still contain software viruses which could damage your computer system:
> you are advised to perform your own checks.
> 
> Email communications with The University of Nottingham Ningbo China may be 
> monitored as permitted by UK and Chinese legislation.

http://www.ccs.neu.edu/home/matthias/



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to