scheme/racket: canvas manipulation

2019-05-07 08:49发布

问题:

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

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

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))

回答1:

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) 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)))

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

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 .



标签: scheme racket