views:

33

answers:

2

Hello, how can I draw strings onto BitmapData, is there something like Java´s Graphics.drawString()?

+2  A: 

In Actionscript, the most natural way of handling this, I think, would be using a container such as Sprite and drawing using it's graphics object and / or adding other display objects as children. Then you could take your "snapshot" when / if necessary, to get the pixel data.

For adding text, creating a TextField is the simplest option.

Anyway, you could write a little function that does this on an existing BitmapData, if you wanted. Here's a sketch of how such a function could be written:

function drawString(target:BitmapData,text:String,x:Number,y:Number):void {
    var tf:TextField = new TextField();
    tf.text = text; 
    var bmd:BitmapData = new BitmapData(tf.width,tf.height);
    bmd.draw(tf);
    var mat:Matrix = new Matrix();
    mat.translate(x,y);
    target.draw(bmd,mat);
    bmd.dispose();
}

// use
var bitmap:BitmapData = new BitmapData(400,400);
// let's draw something first (whatever is on the stage at this point)
bitmap.draw(stage);
drawString(bitmap,"testing",100,50);
// display the result...
addChild(new Bitmap(bitmap));
Juan Pablo Califano
A: 

You can draw a TextField into your bitmap :

import flash.text.TextField;
import flash.display.BitmapData;
import flash.display.Bitmap;

var tf:TextField=new TextField();
tf.text="Hello world";
var bd:BitmapData=new BitmapData(200,200, false,0x00ff00);
bd.draw(tf);
var bm:Bitmap=new Bitmap(bd);
addChild(bm);
Patrick