09-27-2007, 11:48 PM
|
#1
|
|
Registered User
Join Date: Sep 2007
Posts: 34
|
write text on canvas
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
|
|
|
09-27-2007, 11:49 PM
|
#2
|
|
Registered User
Join Date: Sep 2007
Posts: 34
|
write text on canvas
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
|
|
|
09-28-2007, 12:27 AM
|
#3
|
|
Flexpert
Join Date: Sep 2006
Location: Seattle, WA: USA
Posts: 1,555
|
You need to create one of the text (Text, TextArea, Label, UITextField) components and position it in the desired location.
ActionScript Code:
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
Last edited by drkstr; 09-28-2007 at 12:39 AM.
|
|
|
09-28-2007, 04:31 AM
|
#4
|
|
Registered User
Join Date: Sep 2007
Posts: 34
|
Parameter antiAliasType must be non-null
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()
|
|
|
09-28-2007, 05:53 AM
|
#5
|
|
Senior Member
Join Date: Mar 2006
Posts: 464
|
you can use the Text class or Label class
var label:Label = new Label();
label.text ="hi";
canvas.addChild(label);
http://www.jessecouch.com
|
|
|
09-28-2007, 12:38 PM
|
#6
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
Please keep your question to one thread... Crossposts merged...
|
|
|
09-28-2007, 10:19 PM
|
#7
|
|
Flexpert
Join Date: Sep 2006
Location: Seattle, WA: USA
Posts: 1,555
|
Code:
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
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 04:20 PM.
///
|
|