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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Dec 18 17:48:19 EST 2007

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.