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