Drawing onto canvas% element

我是研究僧i 提交于 2019-12-22 06:56:01

问题


I have a problem while trying to draw onto a canvas GUI element.

I create a frame, a canvas and try to draw on the dc context of the canvas with the draw-line method, but nothing happens. The frame with the canvas is shown, but the line isn't shown on the canvas.

(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 dc draw-line 10 10 200 200)
(send frame show #t)

Does anybody know where I am wrong in the code above ?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16084690/drawing-onto-canvas-element

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