<div dir="ltr">Hi,<div><br></div><div>Just make the drawing code part of on-paint. I have inlined a subclass for you:</div><div><br></div><div><div>#lang racket/gui</div><div><br></div><div>(define frame (new frame%</div><div>                   [label "Example"]</div><div>                   [width 300]</div><div>                   [height 300]))</div><div>(define top-canvas (new (class canvas% (super-new [parent frame])</div><div>                          (define dc (send this get-dc))</div><div>                          </div><div>                          (define/override (on-paint)</div><div>                            (send dc draw-rectangle</div><div>                                  0  10   ; Top-left at (0, 10), 10 pixels down from top-left</div><div>                                  30 10) ; 30 pixels wide and 10 pixels high</div><div>                            (send dc draw-line</div><div>                                  0 0    ; Start at (0, 0), the top-left corner</div><div>                                  30 30) ; and draw to (30, 30), the bottom-right corner</div><div>                            (send dc draw-line</div><div>                                  0 30   ; Start at (0, 30), the bottom-left corner</div><div>                                  30 0)  ; and draw to (30, 0), the top-right corner</div><div>                            ))))</div><div><br></div><div>(send frame show #t)</div></div><div><br></div><div>But if you want, you can do something like</div><div><br></div><div>(define your-canvas</div><div>  (class canvas%</div><div>     ... ; as-above after the class canvas% part</div><div><br></div><div>Also you can combine commands to a single object with send*</div><div><br></div><div><div>(define/override (on-paint)</div><div>                            (send* dc</div><div>                              (draw-rectangle</div><div>                               0  10   ; Top-left at (0, 10), 10 pixels down from top-left</div><div>                               30 10) ; 30 pixels wide and 10 pixels high</div><div>                              (draw-line</div><div>                               0 0    ; Start at (0, 0), the top-left corner</div><div>                               30 30) ; and draw to (30, 30), the bottom-right corner</div><div>                              (draw-line</div><div>                               0 30   ; Start at (0, 30), the bottom-left corner</div><div>                               30 0)  ; and draw to (30, 0), the top-right corner</div><div>                              )))))</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 1, 2014 at 9:51 PM, Chris Wright <span dir="ltr"><<a href="mailto:cawright.99@gmail.com" target="_blank">cawright.99@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>I would like to draw on a canvas in a window at various times during program execution. <br></div>My first attempt was to combine two examples:<br><br>#lang racket/gui<br><br>(define frame (new frame%<br>                   [label "Example"]<br>                   [width 300]<br>                   [height 300]))<br>(define top-canvas (new canvas% [parent frame]))<br><br>(send frame show #t)<br><br>(define dc (send top-canvas get-dc))<br><br>(send dc draw-rectangle<br>      0  10   ; Top-left at (0, 10), 10 pixels down from top-left<br>      30 10) ; 30 pixels wide and 10 pixels high<br>(send dc draw-line<br>      0 0    ; Start at (0, 0), the top-left corner<br>      30 30) ; and draw to (30, 30), the bottom-right corner<br>(send dc draw-line<br>      0 30   ; Start at (0, 30), the bottom-left corner<br>      30 0)  ; and draw to (30, 0), the top-right corner<br><br>                      <br><br></div>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..)<br></div>If I put the (send frame...) form at the end of the code, the cross and box aren't seen.<br><br></div>I am sure this is due to me not understanding the model properly - I'd be grateful for some help...<br><br></div>many thanks<span class="HOEnZb"><font color="#888888"><br><br>Chris<br></font></span></div>
<br>____________________<br>
  Racket Users list:<br>
  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div>