BufferedBitmap not working for me

I'm writing an app were I want to overlay several bitmaps before I display it. BufferedBitmap seems the way to go. I got something to work but not the way described in the documentation. Here is a code snippet in onLayout(dc) of what I am doing:
// set up buffer for the court
offscreenBuffer = new Graphics.BufferedBitmap(
{:width=>200,
:height=>140,
// :bitmapRescource=>court // for some reason this doesn't work ???
} ); // create an off-screen buffer to update the court
bufdc = offscreenBuffer.getDc();
court.draw(bufdc); // works successfully
// bufdc.drawBitmap(20, 85, Ones2); for some reason this doesn't work
Ones2.setLocation(20, 85); // set the location
Ones2.draw(bufdc); //overlays successfully ???
1st issue I have is defining the bitmapResource in the BufferedBitmap. It doesn't display, however if I define it with out the resource and then do a draw() function, it works.
2nd issue I have is I can't get drawBitmap() to work. When I set the x, y locations and the resource nothing displays. When I use the setLocation() and draw() functions, it works!
I must not be understanding some underling principle here, any help would be appreciated.
  • Former Member
    Former Member
    If "foo.draw(bufdc)" works, this indicates that foo is a WatchUi.Bitmap (Which extends WatchUi.Drawble).

    The BufferedBitmap constructor requires a BitmapResource for the :bitmapResource option, and Dc.drawBitmap() takes a BitmapResource, or a BufferedBitmap for the 3rd argument. Neither of these cases accept a Drawable.

    Instead of declaring new WatchUi.Bitmap using the "rezId" of your bitmap, and attempting to pass the object to the buffered bitmap constructor, you will need to use WatchUi.loadResource() to load the bitmap resource from the rezId, and provide that to the constructor.

    Similarly, you can load the Ones2 resource and pass that to drawBitmap instead of using the Drawable.draw() routine if you prefer. The method you are using should work fine also. It may use slightly more memory due to a bit of extra overhead in the WatchUi.Bitmap object.
  • Thanks Brian, that is understandable and answers almost all my questions. I re-coded and everything looks good except I'm not understanding the buffered bitmap constructor.
    The png file is 200x140.
    resource file:
    <bitmap id="id_court" filename="images/court.png" />

    code:
    var court = WatchUi.loadResource(Rez.Drawables.id_court);

    var offscreenBuffer = new Graphics.BufferedBitmap(
    {:width=>200,
    :height=>140,
    :bitmapRescource=>court
    } ); // create an off-screen buffer to update the court

    dc.drawBitmap(20, 50, offscreenBuffer); // doesn't display court unless I do the two lines below first.

    I have to do this for it to display.

    bufdc = offscreenBuffer.getDc();
    bufdc.drawBitmap(0, 0, court);

    I would have thought declaring the buffered bitmap with the resource would have loaded the bitmap into the buffer?
  • Former Member
    Former Member
    It should work as you are expecting it to. If this is a direct copy of your code, this is a typo: bitmapRescource (bitmapResource)

    The width/height options are ignored if a resource is provided to the constructor, and can be removed. (I thought this was documented, but it appears it is not.)
  • Yep, that's what it was, a typo! Thanks Brian for the extra set of eyes.