[plt-scheme] Calling Superclass Methods

From: Doug Williams (m.douglas.williams at gmail.com)
Date: Sun Dec 9 21:48:58 EST 2007

I am trying to create an animated-canvas% class that has two bitmaps that I
swap for some simple animation.  I have the code working with a straight
canvas% instance (with the bitmaps, and dcs in variables), but wanted to
abstract it into a subclass.  The problem is that I haven't figured out how
to override the get-dc (to return a dc for one of the bitmaps) and still
call the superclass's get-dc to draw the bit map on the canvas.  Any other
suggestions on the approach would also be welcome.

(module animated-canvas mzscheme

  (require (lib "class.ss"))
  (require (lib "mred.ss" "mred"))
  (require (lib "etc.ss"))

  (provide (all-defined))

  (define animated-canvas%
    (class canvas%
      (inherit refresh)
      (inherit get-client-size)
      (inherit min-client-height)
      (inherit min-client-width)
      (super-instantiate ())
      (define bitmap-vector
        (let ((w (min-client-width))
              (h (min-client-height)))
          ;(printf "width = ~a, height = ~a~n" w h)
          (build-vector
           2
           (lambda (i)
             (make-object bitmap% w h)))))
      (define from-bitmap 0)
      (define to-bitmap 1)
      (define from-bitmap-dc (make-object bitmap-dc%))
      (define to-bitmap-dc (make-object bitmap-dc%))
      (define/public (swap-bitmaps)
        ;; Reset bitmap-dc instances.
        (send from-bitmap-dc set-bitmap #f)
        (send to-bitmap-dc set-bitmap #f)
        ;; Swap bitmaps.
        (set! from-bitmap (modulo (+ from-bitmap 1) 2))
        (set! to-bitmap (modulo (+ to-bitmap 1) 2))
        ;; Set bitmap-dc instances.
        (send from-bitmap-dc set-bitmap (vector-ref bitmap-vector
from-bitmap))
        (send to-bitmap-dc set-bitmap (vector-ref bitmap-vector to-bitmap))
        ;;
        (refresh))
      (rename (super-get-dc get-dc)) ; this doesn't work, for example
      (define/override (get-dc)
        (printf "animated-canvas% get-dc~n")
        to-bitmap-dc)
      (define/override (on-paint)
        (let ((canvas-dc (super-get-dc))) ; here is where I want to call the
get-dc method for the superclass
          (send canvas-dc draw-bitmap (vector-ref bitmap-vector from-bitmap)
0 0)))
        ))

  )

Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20071209/6f38700f/attachment.html>

Posted on the users mailing list.