PDA

View Full Version : write text on canvas


finecur
09-28-2007, 12:48 AM
Hi,

I am a Java programmer and new to flex/actionscript. I am trying to find a way to draw a circle on canvas and then draw a text "hello" in the center of the cicle.

In java, you can use:

graphics.drawCircle(100, 100, 50);
graphics.drawText("Hello", 100, 100);

In flex/actionscript, we have

canvas.graphics.drawCircle(100, 100, 50);

But I can not find the equivalent to draw text.

Can anyone help?

Thanks,

qq

finecur
09-28-2007, 12:49 AM
Hi,

I am a Java programmer and new to flex/actionscript. I am trying to find a way to draw a circle on canvas and then draw a text "hello" in the center of the cicle.

In java, you can use:

graphics.drawCircle(100, 100, 50);
graphics.drawText("Hello", 100, 100);

In flex/actionscript, we have

canvas.graphics.drawCircle(100, 100, 50);

But I can not find the equivalent to draw text.

Can anyone help?

Thanks,

qq

drkstr
09-28-2007, 01:27 AM
You need to create one of the text (Text, TextArea, Label, UITextField) components and position it in the desired location.

private function drawText( text:String, centerX:int, centerY:int ): void {
var label:Label= new Label();
var metrics:TextLineMetrics = label.measureText(text);

label.width = metrics.width;
label.height = metrics.height;
label.x = centerX - Math.round(label.width/2);
label.y = centerY - Math.round(label.height/2);

addChild(label);
}


Best Regards,
...aaron

finecur
09-28-2007, 05:31 AM
Thank you for the information.

However, I got the follow error:

TypeError: Error #2007: Parameter antiAliasType must be non-null.
at flash.text::TextField/set antiAliasType()
at mx.core::UITextFormat/::measure()
at mx.core::UITextFormat/measureText()
at mx.core::UIComponent/measureText()
at VisImage/::drawCenter()

springframework
09-28-2007, 06:53 AM
you can use the Text class or Label class

var label:Label = new Label();
label.text ="hi";
canvas.addChild(label);


http://www.jessecouch.com

CyanBlue
09-28-2007, 01:38 PM
Please keep your question to one thread... Crossposts merged... :(

drkstr
09-28-2007, 11:19 PM
Thank you for the information.

However, I got the follow error:

TypeError: Error #2007: Parameter antiAliasType must be non-null.
at flash.text::TextField/set antiAliasType()
at mx.core::UITextFormat/::measure()
at mx.core::UITextFormat/measureText()
at mx.core::UIComponent/measureText()
at VisImage/::drawCenter()

Yeah, I got that once too awhile back. It has something to do with the initialization of the text component and there are a few different things that can cause it. What is the component type of the parent? You may need to override the createChildren method and addChild there. The most important thing is that you add it to a component that is in the display list, or force it to initialize with a call to label.regenerateStyleCache(false)

Hope this helps, let me know if it doesn't and I'll try to dig up my old code.

Best Regards,
...aaron