方案/球拍:画布操作 [英] scheme/racket: canvas manipulation

查看:36
本文介绍了方案/球拍:画布操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1) 正如标题所说,当我调整窗口大小时,我绘制的对象消失了,但矩形保持原样.

1) As title says, the objects i draw disappear when i resize the window, but the rectangle stays as is.

2) 原点从左上角开始,但我希望它在左下角.

2) The origin starts from the top left, but i wish for it to be at the bottom left.

3) 除了绘图库之外,我找不到任何缩放功能,所以如果我想实现这样的功能,一个选择是通过绘制更大的对象并刷新画布来放大"?

3) I couldn't find any zoom functions, other than in the plot library so if i wish to implement such a thing, one option would to be "zooming" in by drawing bigger objects and refreshing the canvas instead?

(define top-frame (new frame%
                         [label "KR"]
                         [width 500]
                         [height 500]))
;Make a frame by instantiating the frame% class

(define image (pict->bitmap (rectangle 50 50)))

(define canvas (new canvas%
                    [parent top-frame]
                    [paint-callback (lambda (canvas dc)
                                      (send dc draw-bitmap image 0 0))]))

(define drawer (send canvas get-dc))

(send top-frame show #t)
; Show the frame by calling its show method

(define (draw-object x)
  (sleep/yield 0.1)
  (case (first x) 
    [("LINE") (send drawer draw-line
                    (second x) (third x)
                    (fourth x) (fifth x))]
    [("CIRCLE") (send drawer draw-bitmap (pict->bitmap (circle (round (fourth x)))) (round (second x)) (round (third x)))]
    [("POINT") (send drawer draw-point (round (second x)) (round (third x)))]
    [else "Not drawing anything!"]))

(draw-object (find-specific-values (third list-of-objects)))
(map draw-object (map find-specific-values list-of-objects))

推荐答案

ad 1) ...当我调整窗口大小时,我绘制的对象消失了,..."当您调整窗口大小时,系统需要重新绘制窗口的内容.发出重绘事件,最终 Racket GUI 层将调用paint-callback.因此:创建一个可以完成所有绘图的函数.从油漆回调中调用它.在此处查看类似问题:https://stackoverflow.com/a/16086594/23567

ad 1) "...the objects i draw disappear when i resize the window, ..." When you resize a window the system needs to redraw the contents of the window. A redraw event is issued, and eventually the Racket GUI layer will call the paint-callback. Therefore: Make a function that does all the drawing. Call it from the paint-callback. See similar question here: https://stackoverflow.com/a/16086594/23567

ad 2) 一种选择是在绘图上下文中进行坐标变换.请参阅 dc<%> 文档中的 set-transformation.是这样的:

ad 2) One option is to make a coordinate transformation in the drawing context. See set-transformation in the docs for dc<%>. It's someething like this:

(send dc set-transformation 
        (vector (trans->vector t)
                0   0 ; x and y origin
                1  -1 ; x and y scale
                0)))

y 尺度的 -1 将翻转 y 轴.您可能想要移动原点.

The -1 for the y-scale will flip the y-axis. You might want to move the origin.

ad 3) 缩放可以通过更改 x 和 y 比例,然后重新绘制来完成.你可以试试 1/2 -1/2 的音阶.

ad 3) Zooming can be done by changing the x and y scale, and then redrawing. You can try the scales to 1/2 -1/2 .

这篇关于方案/球拍:画布操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆