[plt-scheme] Calling Superclass Methods
I think you probably want to leave the get-dc method alone and have
instead a get-current-bitmap-dc or similar that you call from your
drawing routines and whose result differs. Then, the on-paint method
can use get-dc to get the actual dc for the screen, and then draw the
appropriate bitmap into it.
Would that work?
Robby
On Dec 9, 2007 8:48 PM, Doug Williams <m.douglas.williams at gmail.com> wrote:
> 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
>
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>