<p dir="ltr">You should learn about Model-View-Controller architecture (for example at <a href="http://en.m.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">http://en.m.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller</a>).</p>
<p dir="ltr">Briefly, the model is a data structure that represents the information you are working with. The view is able to produce a visual representation of the model. A controller is used to update the model in response to input.</p>
<p dir="ltr">In your example, the canvas is the view. Your on-draw method needs to draw the model. Your buttons will send a message to your controller, and the controller will update the model accordingly. Then, when on-draw is called, it will draw the updated model.</p>
<p dir="ltr">Justin</p>
<div class="gmail_quote">On Oct 1, 2014 10:17 PM, "Chris Wright" <<a href="mailto:cawright.99@gmail.com">cawright.99@gmail.com</a>> wrote:<br type="attribution"><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>Thanks Matthias and Sean<br></div><br></div>It's often helpful when people asking questions ask them clearly ! :)<br></div>I'll now attempt a clarification...<br><br>say I have a button in the window, and when that button is pressed, I want to draw on the canvas - and later, another button is pressed, and I might want to draw something else somewhere else on the canvas.<br><br>I think I'm wanting to get hold of the dc outside of the definition of on-paint in the initialisation code <br><br></div>cheers and thanks again<br><br>Chris<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 2 October 2014 11:57, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Does this help?<br>
<span><br>
#lang racket/gui<br>
<br>
(define frame<br>
(new frame%<br>
[label "Example"]<br>
[width 300]<br>
[height 300]))<br>
<br>
(define top-canvas<br>
</span> (new (class canvas%<br>
(inherit get-dc)<br>
(super-new [parent frame])<br>
(define/override (on-paint)<br>
(define dc (get-dc))<br>
<span> (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>
</span> ))))<br>
<br>
(send frame show #t)<br>
<div><div><br>
<br>
<br>
<br>
On Oct 1, 2014, at 9:51 PM, Chris Wright wrote:<br>
<br>
> I would like to draw on a canvas in a window at various times during program execution.<br>
> 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>
> 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>
> If I put the (send frame...) form at the end of the code, the cross and box aren't seen.<br>
><br>
> I am sure this is due to me not understanding the model properly - I'd be grateful for some help...<br>
><br>
> many thanks<br>
><br>
> Chris<br>
</div></div>> ____________________<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><br clear="all"><br>-- <br>A/Prof Chris Wright<br>MBBS, FRACP, FCICM, GradDipiSc(Physics)<br>Academic Coordinator, Years III - V Central MBBS<br>Intensive Care Specialist<br>Faculty of Medicine, Nursing and Health Sciences,<br>Monash University<br>Clayton VIC
</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>