[plt-scheme] drawing open lines

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Thu May 14 21:27:42 EDT 2009

Just make the brush be transparent (in the paint callback):

(send dc set-brush "black" 'transparent)

Also, you shouldn't call on-paint; call refresh instead. I also made a
little change so you can see the path you're drawing.

Robby


#lang scheme/gui
(define frame (new frame%
                  [label "Squiggles"]
                  [width 300]
                  [height 300]))

;; A canvas you can draw squiggles on.
(define squiggle-canvas%
 (class canvas%
   (define squiggling? false)
   (define points empty)  ;; Listof Point
   (define strokes empty) ;; Listof [Listof Point]

   (define/public (draw-strokes)
     (let ([dc (get-dc)])
       (for-each (lambda (s) (send dc draw-path (stroke->path s)))
                 strokes)))

   ;; [Listof Point] -> Path
   (define (stroke->path s)
     (let ((path (make-object dc-path%)))
       (send path move-to
             (send (first s) get-x)
             (send (first s) get-y))
       (send path lines s)
       path))

   (inherit refresh)
   (define/override (on-event ev)
     (cond [(and (not squiggling?)
                 (send ev dragging?))
            ; start sguiggling
            (set! squiggling? true)
            (set! points empty)]
           [(and squiggling?
                 (send ev dragging?))
            ; continue squiggling
            (set! points
                  (cons (make-object point%
                          (send ev get-x)
                          (send ev get-y))
                        points))
            (refresh)]
           [squiggling?
            ; stop squiggling (other event)
            (unless (empty? points)
              (set! strokes (cons points strokes)))
            (set! squiggling? false)
            (refresh)]
           [else (void)]))

   (inherit get-dc)
   (define/override (on-paint)
     (let ([dc (get-dc)])
       (send dc set-smoothing 'smoothed)
       (send dc set-pen "red" 3 'dot)
       (send dc set-brush "black" 'transparent)
       (unless (null? points)
         (send dc draw-path (stroke->path points)))
       (draw-strokes)))

   (super-new)))

; Make the drawing area
(define canvas
 (new squiggle-canvas%
      [parent frame]
      [style '(no-autoclear)]))

(send frame show #t)


On Thu, May 14, 2009 at 8:19 PM, David Van Horn <dvanhorn at ccs.neu.edu> wrote:
> I'm trying to draw a list of lines on a canvas, but the lines seem to be
> implicitly closed so that further drawing within the area closed by the
> lines does not appear.
>
> Attached is a sample program.  Draw a circle (click and drag the mouse on
> the canvas), then draw a line through it to see the effect.
>
> How can I draw lines without this closing behavior?
>
> Thanks,
> David
>
> #lang scheme/gui
> (define frame (new frame%
>                   [label "Squiggles"]
>                   [width 300]
>                   [height 300]))
>
> ;; A canvas you can draw squiggles on.
> (define squiggle-canvas%
>  (class canvas%
>    (define squiggling? false)
>    (define points empty)  ;; Listof Point
>    (define strokes empty) ;; Listof [Listof Point]
>
>    (define/public (draw-strokes dc)
>      (for-each (lambda (s) (send dc draw-path (stroke->path s)))
>                strokes))
>
>    ;; [Listof Point] -> Path
>    (define (stroke->path s)
>      (let ((path (make-object dc-path%)))
>        (send path move-to
>              (send (first s) get-x)
>              (send (first s) get-y))
>        (send path lines s)
>        path))
>
>    (define/override (on-event ev)
>      (cond [(and (not squiggling?)
>                  (send ev dragging?))
>             ; start sguiggling
>             (set! squiggling? true)
>             (set! points empty)]
>            [(and squiggling?
>                  (send ev dragging?))
>             ; continue squiggling
>             (set! points
>                   (cons (make-object point%
>                           (send ev get-x)
>                           (send ev get-y))
>                         points))]
>            [squiggling?
>             ; stop squiggling (other event)
>             (unless (empty? points)
>               (set! strokes (cons points strokes)))
>             (set! squiggling? false)
>             (send this on-paint)]
>            [else (void)]))
>
>    (super-new)))
>
> ; Make the drawing area
> (define canvas
>  (new squiggle-canvas%
>       [parent frame]
>       [style '(no-autoclear)]
>       [paint-callback
>        (lambda (canvas dc)
>          (send dc set-smoothing 'smoothed)
>          (send dc set-pen "red" 3 'dot)
>          (send canvas draw-strokes dc))]))
>
> (send frame show #t)
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.