[plt-scheme] Question on Canvas object in GUI programming

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Dec 18 22:04:45 EST 2007

Now it's time to use the help desk and to search for terms like  
canvas, editor, pasteboard, editor-canvas and just chase the links.  
Everything is hyperlinked, so you can find your way around easily,  
not that this means everything is easy. -- Matthias



On Dec 18, 2007, at 10:00 PM, Lian DanHui wrote:

> Hello Matthias,
>
> Really appreciate your answer provided! It is very clear, and I get  
> it.
>
> And I think of other scenarios:
> One possible scene, if the picture is scaled up or down on the canvas?
> One possible scene, if the user do some changes on the canvas, and  
> then click to get the pixel color?
>
> I think for the first scene, I still can use the method you  
> provided. But for second case, I don't know how.
> I wonder if I can get the bitmap-dc% from Canvas dc, or something  
> similar like that.
> Can I get the pixels matrix of the current canvas operated?
>
> thanks & regards,
> Linda Lian
>
> On 12/19/07, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
> On Dec 18, 2007, at 5:08 AM, Lian DanHui wrote:
>
> > Hello,
> >
> > I want to implement something like this:
> >   1. Display a picture in a Canvas on a Frame;
> >   2. User can click on the picture;
> >   3. I want to get the color of the pixel that user clicked;
> >
> > For Canvas object,  Using what method or what strategy, that I can
> > get the pixel info?
> > Thanks a lot for the help!
>
> Here is a literal translation of your request into the most basic
> language. If you are a student and using world.ss, it is simpler. --
> Matthias
>
> #lang scheme/gui
>
> ;; obtain a bitmap and stick it into a bitmap dc
> (define plt
>   (make-object bitmap% (build-path (collection-path "icons")
> "plt.gif")))
> (define bm
>   (new bitmap-dc% [bitmap plt]))
>
> ;; --- set up a frame with a canvas in which to display this image
> (define frame
>   (new frame% [label "Test for Linda"]
>        [width (send plt get-width)] [height (send plt get-height)]))
>
> (define canvas (new
>                 ;; --- create a class on the fly that overrides the
> callback for
>                 ;; --- mouse events in a canvas; you could name it,
> if needed
>                 (class canvas%
>                   ;; Event -> Void
>                   ;; act on each mouse event
>                   (define/override (on-event e)
>                     (define kind (send e button-down?))
>                     (define x (send e get-x))
>                     (define y (send e get-y))
>                     (define c (new color%))
>                     (when kind
>                       (if (send bm get-pixel x y c)
>                           (printf "color is ~s ~s ~s\n"
>                                   (send c red) (send c green) (send
> c blue))
>                           (printf "no color"))))
>                   (super-new))
>                 ;;--- provide a frame and a paint method for
> displaying plt.gif
>                 [parent frame]
>                 [paint-callback (lambda (e dc) (send dc draw-bitmap
> plt 0 0))]))
>
> ;; --- run program run
> (send frame show #t)
>



Posted on the users mailing list.