Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-27-2007, 11:48 PM   #1
finecur
Registered User
 
Join Date: Sep 2007
Posts: 34
Default 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
finecur is offline   Reply With Quote
Old 09-27-2007, 11:49 PM   #2
finecur
Registered User
 
Join Date: Sep 2007
Posts: 34
Default 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
finecur is offline   Reply With Quote
Old 09-28-2007, 12:27 AM   #3
drkstr
Flexpert
 
drkstr's Avatar
 
Join Date: Sep 2006
Location: Seattle, WA: USA
Posts: 1,587
Default

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.
drkstr is offline   Reply With Quote
Old 09-28-2007, 04:31 AM   #4
finecur
Registered User
 
Join Date: Sep 2007
Posts: 34
Default 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()
finecur is offline   Reply With Quote
Old 09-28-2007, 05:53 AM   #5
springframework
Senior Member
 
Join Date: Mar 2006
Posts: 470
Default

you can use the Text class or Label class

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


http://www.jessecouch.com
springframework is offline   Reply With Quote
Old 09-28-2007, 12:38 PM   #6
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Please keep your question to one thread... Crossposts merged...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 09-28-2007, 10:19 PM   #7
drkstr
Flexpert
 
drkstr's Avatar
 
Join Date: Sep 2006
Location: Seattle, WA: USA
Posts: 1,587
Default

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
drkstr is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selected text formation is not working in dynamic text box. trytestabc ActionScript 2.0 1 09-25-2007 09:01 AM
use same LoadVars for different text in different text boxes snowgirl ActionScript 2.0 10 04-09-2007 06:32 PM
Controlling Dynamic Text length fahadbht ActionScript 2.0 5 02-02-2007 11:14 PM
External text loads partially & Render text as HTML prevents loading of text file piercedwater ActionScript 2.0 20 03-02-2005 11:33 PM
Write to text file/ Read from text file? dustbuster ActionScript 1.0 (and below) 3 07-17-2002 06:20 AM


All times are GMT. The time now is 08:08 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.