Drawing onto canvas% element

三世轮回 提交于 2019-12-05 13:01:10

Try this:

(require racket/gui/base)

(define frame (new frame% [label "Frame"] [width 500] [height 500]))
(define canvas (new canvas% [parent frame]))
(define dc (send canvas get-dc))

(send frame show #t)
(sleep/yield 1)
(send dc draw-line 10 10 200 200)

It seems that you need to show the frame first and then wait a bit to let the window get ready.

The problem is that even though you can draw on the canvas outside a call to on-paint method of the canvas, the effect is temporary. Any window activity that require the window to refresh (such as moving, and resizing) can potentially erase your drawing.

Therefore: Draw everything from within the paint-callback.

#lang racket
(require racket/gui/base)

(define frame (new frame% [label "Frame"] [width 500] [height 500]))
(define canvas (new canvas% 
                    [parent frame]
                    [paint-callback 
                     (λ(can dc) (send dc draw-line 10 10 200 200))]))
(define dc (send canvas get-dc))
(send frame show #t)

See Documentation on the canvas class for further information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!