<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Chris,<br>
<br>
It sounds like your mental model of the canvas is that stuff you
draw on it stays there permanently, like paint on a real-world
canvas. This is not how the GUI works. When the GUI canvas is
displayed on the screen, it asks your program (by calling on-paint)
to draw the canvas contents. Lots of things can trigger an on-paint
- resizing the canvas, another window moving across the canvas, the
laptop resuming from sleep, etc. You can trigger one in your code
by calling refresh. See below for Matthias' example with a button
added.<br>
<br>
Does this help?<br>
<br>
Thanks,<br>
Dave<br>
<br>
<tt>#lang racket/gui</tt><tt><br>
</tt><tt><br>
</tt><tt>(define show-rectangle? #f)</tt><tt><br>
</tt><tt><br>
</tt><tt>(define frame</tt><tt><br>
</tt><tt> (new frame%</tt><tt><br>
</tt><tt> [label "Example"]</tt><tt><br>
</tt><tt> [width 300]</tt><tt><br>
</tt><tt> [height 300]))</tt><tt><br>
</tt><tt><br>
</tt><tt>(define top-canvas</tt><tt><br>
</tt><tt> (new (class canvas%</tt><tt><br>
</tt><tt> (inherit get-dc)</tt><tt><br>
</tt><tt> (super-new [parent frame])</tt><tt><br>
</tt><tt> (define/override (on-paint)</tt><tt><br>
</tt><tt> (define dc (get-dc))</tt><tt><br>
</tt><tt> (when show-rectangle?</tt><tt><br>
</tt><tt> (send dc draw-rectangle</tt><tt><br>
</tt><tt> 0 10 ; Top-left at (0, 10), 10 pixels
down from top-left</tt><tt><br>
</tt><tt> 30 10)) ; 30 pixels wide and 10 pixels
high</tt><tt><br>
</tt><tt> (send dc draw-line</tt><tt><br>
</tt><tt> 0 0 ; Start at (0, 0), the top-left
corner</tt><tt><br>
</tt><tt> 30 30) ; and draw to (30, 30), the
bottom-right corner</tt><tt><br>
</tt><tt> (send dc draw-line</tt><tt><br>
</tt><tt> 0 30 ; Start at (0, 30), the bottom-left
corner</tt><tt><br>
</tt><tt> 30 0) ; and draw to (30, 0), the
top-right corner</tt><tt><br>
</tt><tt> ))))</tt><tt><br>
</tt><tt><br>
</tt><tt>(define top-button</tt><tt><br>
</tt><tt> (new button%</tt><tt><br>
</tt><tt> [parent frame]</tt><tt><br>
</tt><tt> [label "Show Rectangle"]</tt><tt><br>
</tt><tt> [callback (lambda (b e)</tt><tt><br>
</tt><tt> (set! show-rectangle? #t)</tt><tt><br>
</tt><tt> (send top-canvas refresh))]))</tt><tt><br>
</tt><tt><br>
</tt><tt>(send frame show #t)</tt><br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 10/01/2014 10:16 PM, Chris Wright
wrote:<br>
</div>
<blockquote
cite="mid:CABke0-UNTT_ieeAWcPf8SK-S0xyLR=O+EZrXSCVAVR62XNjebA@mail.gmail.com"
type="cite">
<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 moz-do-not-send="true"
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 class=""><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 class=""> (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 class="h5"><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 moz-do-not-send="true"
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>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">____________________
Racket Users list:
<a class="moz-txt-link-freetext" href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a>
</pre>
</blockquote>
<br>
</body>
</html>