Chris,
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.
Does this help?
Thanks,
Dave
#lang racket/gui
(define show-rectangle? #f)
(define frame
(new frame%
[label "Example"]
[width 300]
[height 300]))
(define top-canvas
(new (class canvas%
(inherit get-dc)
(super-new [parent frame])
(define/override (on-paint)
(define dc (get-dc))
(when show-rectangle?
(send dc draw-rectangle
0 10 ; Top-left at (0, 10), 10 pixels down from
top-left
30 10)) ; 30 pixels wide and 10 pixels high
(send dc draw-line
0 0 ; Start at (0, 0), the top-left corner
30 30) ; and draw to (30, 30), the bottom-right corner
(send dc draw-line
0 30 ; Start at (0, 30), the bottom-left corner
30 0) ; and draw to (30, 0), the top-right corner
))))
(define top-button
(new button%
[parent frame]
[label "Show Rectangle"]
[callback (lambda (b e)
(set! show-rectangle? #t)
(send top-canvas refresh))]))
(send frame show #t)
On 10/01/2014 10:16 PM, Chris Wright wrote:
Thanks Matthias and Sean
It's often helpful when people asking questions ask them clearly ! :)
I'll now attempt a clarification...
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.
I think I'm wanting to get hold of the dc outside of the definition of
on-paint in the initialisation code
cheers and thanks again
Chris
On 2 October 2014 11:57, Matthias Felleisen <matth...@ccs.neu.edu
<mailto:matth...@ccs.neu.edu>> wrote:
Does this help?
#lang racket/gui
(define frame
(new frame%
[label "Example"]
[width 300]
[height 300]))
(define top-canvas
(new (class canvas%
(inherit get-dc)
(super-new [parent frame])
(define/override (on-paint)
(define dc (get-dc))
(send dc draw-rectangle
0 10 ; Top-left at (0, 10), 10 pixels down
from top-left
30 10) ; 30 pixels wide and 10 pixels high
(send dc draw-line
0 0 ; Start at (0, 0), the top-left corner
30 30) ; and draw to (30, 30), the bottom-right
corner
(send dc draw-line
0 30 ; Start at (0, 30), the bottom-left corner
30 0) ; and draw to (30, 0), the top-right corner
))))
(send frame show #t)
On Oct 1, 2014, at 9:51 PM, Chris Wright wrote:
> I would like to draw on a canvas in a window at various times
during program execution.
> My first attempt was to combine two examples:
>
> #lang racket/gui
>
> (define frame (new frame%
> [label "Example"]
> [width 300]
> [height 300]))
> (define top-canvas (new canvas% [parent frame]))
>
> (send frame show #t)
>
> (define dc (send top-canvas get-dc))
>
> (send dc draw-rectangle
> 0 10 ; Top-left at (0, 10), 10 pixels down from top-left
> 30 10) ; 30 pixels wide and 10 pixels high
> (send dc draw-line
> 0 0 ; Start at (0, 0), the top-left corner
> 30 30) ; and draw to (30, 30), the bottom-right corner
> (send dc draw-line
> 0 30 ; Start at (0, 30), the bottom-left corner
> 30 0) ; and draw to (30, 0), the top-right corner
>
>
>
> 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..)
> If I put the (send frame...) form at the end of the code, the
cross and box aren't seen.
>
> I am sure this is due to me not understanding the model properly
- I'd be grateful for some help...
>
> many thanks
>
> Chris
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
--
A/Prof Chris Wright
MBBS, FRACP, FCICM, GradDipiSc(Physics)
Academic Coordinator, Years III - V Central MBBS
Intensive Care Specialist
Faculty of Medicine, Nursing and Health Sciences,
Monash University
Clayton VIC
____________________
Racket Users list:
http://lists.racket-lang.org/users
____________________
Racket Users list:
http://lists.racket-lang.org/users