[flexcoders] Re: Writing text directly to Graphics object.
In retrospect, it seems reasonable that one would need to use a drawXxx() command to have beginBitmapFill() affect the graphics port. Anyway, in case this useful to anyone else, here's a method that draws a string to a graphics object: public function drawString(g:Graphics, str:String, x:Number, y:Number):void { var tf:TextField = new TextField(); tf.text = str; var bmd:BitmapData = new BitmapData(tf.textWidth + 6, tf.textHeight, true, 0xFF); bmd.draw(tf); var matrix:Matrix = new Matrix(); matrix.createBox(1, 1, 0, x, y); g.beginBitmapFill(bmd, matrix); g.drawRect(x, y, bmd.width, bmd.height); g.endFill(); } Of course, in actual use, one would probably want to set fonts, colors, etc. --- In flexcoders@yahoogroups.com, Eric Cooper [EMAIL PROTECTED] wrote: I have tried this, but it doesn't seem to be working. Here's what I am doing: override public function renderShape(graphics:Graphics):void { var matrix:Matrix = new Matrix() matrix.createBox(1, 1, 0, this.center.x, this.center.y ); // center is the vertex of an angle graphics.beginBitmapFill(this.bitmapData, matrix); graphics.endFill(); } // use bright colors for debugging. private function generateBitmapData():void { this._textField.text = this.angle + º; this._textField.textColor = 0xFF; this._textField.background = true; this._textField.backgroundColor = 0xFF; _bitmapData = new BitmapData(this._textField.textWidth, this._textField.textHeight, false, 0x00FF00); _bitmapData.draw(this._textField); } private function get bitmapData():BitmapData { if (_bitmapData == null) this.generateBitmapData(); return _bitmapData; } Note that the real renderShape() also draws a pie wedge to the canvas - and the wedge is visible. As an aside, though, I am also having trouble with drawing the pie wedge - the wedge works fine, but if I try to draw just the arc of the wedge, then I find that the arc deforms into a closed shape: the two end points are connected by a line. If I omit beginFill() and endFill() calls, relying only on lineStyle(), then connecting line goes away, but I start seeing strange flood fills. Reading about Graphics, I see that endFill() plays a role in getting draw commands pushed onto draw stack. I also see that the fill specified by beginFill() et al. persists until another beginFill() call. Is there some mantra for turning off fill? Thanks! -Eric --- In flexcoders@yahoogroups.com, Doug McCune doug@ wrote: You can draw a TextField to a BitmapData object (using the draw() method) and then use graphics.beginBitmapFill and pass in that BitmapData (make sure to specify the right matrix for where to start the fill). But no, as far as I know there is not way to do it directly, I often use BitmapData as an intermediary to do stuff like that. Doug On Fri, Apr 11, 2008 at 3:50 PM, Eric Cooper eric@ wrote: Is there any way to write/draw text directly to a Graphics object? For that matter is there anyway to draw text (single line of static text) into a Canvas? I suspect that the answer is no, having searched and searched... but maybe there's some obscure utility class that I've overlooked. Thanks in advance. -Eric
[flexcoders] Re: Writing text directly to Graphics object.
I have tried this, but it doesn't seem to be working. Here's what I am doing: override public function renderShape(graphics:Graphics):void { var matrix:Matrix = new Matrix() matrix.createBox(1, 1, 0, this.center.x, this.center.y ); // center is the vertex of an angle graphics.beginBitmapFill(this.bitmapData, matrix); graphics.endFill(); } // use bright colors for debugging. private function generateBitmapData():void { this._textField.text = this.angle + º; this._textField.textColor = 0xFF; this._textField.background = true; this._textField.backgroundColor = 0xFF; _bitmapData = new BitmapData(this._textField.textWidth, this._textField.textHeight, false, 0x00FF00); _bitmapData.draw(this._textField); } private function get bitmapData():BitmapData { if (_bitmapData == null) this.generateBitmapData(); return _bitmapData; } Note that the real renderShape() also draws a pie wedge to the canvas - and the wedge is visible. As an aside, though, I am also having trouble with drawing the pie wedge - the wedge works fine, but if I try to draw just the arc of the wedge, then I find that the arc deforms into a closed shape: the two end points are connected by a line. If I omit beginFill() and endFill() calls, relying only on lineStyle(), then connecting line goes away, but I start seeing strange flood fills. Reading about Graphics, I see that endFill() plays a role in getting draw commands pushed onto draw stack. I also see that the fill specified by beginFill() et al. persists until another beginFill() call. Is there some mantra for turning off fill? Thanks! -Eric --- In flexcoders@yahoogroups.com, Doug McCune [EMAIL PROTECTED] wrote: You can draw a TextField to a BitmapData object (using the draw() method) and then use graphics.beginBitmapFill and pass in that BitmapData (make sure to specify the right matrix for where to start the fill). But no, as far as I know there is not way to do it directly, I often use BitmapData as an intermediary to do stuff like that. Doug On Fri, Apr 11, 2008 at 3:50 PM, Eric Cooper [EMAIL PROTECTED] wrote: Is there any way to write/draw text directly to a Graphics object? For that matter is there anyway to draw text (single line of static text) into a Canvas? I suspect that the answer is no, having searched and searched... but maybe there's some obscure utility class that I've overlooked. Thanks in advance. -Eric
[flexcoders] Re: Writing text directly to Graphics object.
Yes, this is what I was hoping for. And even though it is not now available, I wonder, Gordon, if you are at liberty to tell us about the future? Is Graphics.drawText() be something that might get added? -eric --- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote: I think the original poster was looking for something like graphics.drawText(Hello, format); But there are (alas!) no APIs in the Graphics class that know how to render a text string. Only TextField knows how to do that. Gordon Smith Adobe Flex SDK Team
RE: [flexcoders] Re: Writing text directly to Graphics object.
Unfortunately, there are no plans to add a text-drawing API like Graphics.drawText() in Flash Player 10. Text will continue to be renderable only by a special type of DisplayObject. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Eric Cooper Sent: Tuesday, April 22, 2008 12:51 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: Writing text directly to Graphics object. Yes, this is what I was hoping for. And even though it is not now available, I wonder, Gordon, if you are at liberty to tell us about the future? Is Graphics.drawText() be something that might get added? -eric --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith [EMAIL PROTECTED] wrote: I think the original poster was looking for something like graphics.drawText(Hello, format); But there are (alas!) no APIs in the Graphics class that know how to render a text string. Only TextField knows how to do that. Gordon Smith Adobe Flex SDK Team