Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » In draw2d, how do you align text about a point?
In draw2d, how do you align text about a point? [message #241166] Thu, 31 January 2008 22:09 Go to next message
Eclipse UserFriend
Originally posted by: aebeytin.yahoo.com

Hi all,

How do you add text to a figure that is aligned around a point? More specifically: I have text and I have an x,y point, but I want the text to have its horizontal (and possibly vertical) center (or possibly right edge) at that point.

My only successful attempt at this so far used an org.eclipse.draw2d.Label, initially positioned at 1,1 inside its containing Figure. Then, a thread was set up to watch it and calculate its correct upper-left-hand corner once it had a known size so that it would be centered around the intended point. This approach caused the text to originally appear in the corner on the creation and each resize of the window, not to mention the overhead of running an extra thread to watch it and all other similar Labels in the Figure. I can't calculate the point earlier because the Label's getSize() method returns an object with zero width and height unless the Figure has already been rendered.

There must be a better solution. Thanks for your help!

Adam
Re: In draw2d, how do you align text about a point? [message #241178 is a reply to message #241166] Fri, 01 February 2008 07:25 Go to previous messageGo to next message
Manuel Selva is currently offline Manuel SelvaFriend
Messages: 189
Registered: July 2009
Location: Grenoble, France
Senior Member
Hi adam,

May be you can try to write your own figure extending rectangle for
exmample and override the fillShape method in order to do.

super.fillShape()
gc.drawString(x, y, myString) where x and y can easily be computed using
this.getBounds (returning this figure's size in width and height fileds)
and using gc.stringExtents method ....

Hope this help

Manu
http://manuelselva.wordpress.com


Re: In draw2d, how do you align text about a point? [message #241188 is a reply to message #241178] Fri, 01 February 2008 21:17 Go to previous message
Eclipse UserFriend
Originally posted by: aebeytin.yahoo.com

Thanks for the help!

I have extended org.eclipse.draw2d.RectangleFigure with a "TextRectangle" that takes a String, an xy point, and a horizontal/vertical alignment. I replaced outlineShape() with an empty function, and I replaced fillShape() with a function that creates a TextLayout and uses its getBounds() function to calculate the correct offsets horizontally and vertically before calling graphics.drawTextLayout(). It works perfectly!

Thanks again,
Adam
Previous Topic:Zest: Open Constraints for TreeLayout and others?
Next Topic:Move children between parents
Goto Forum:
  


Current Time: Sat May 04 09:21:07 GMT 2024

Powered by FUDForum. Page generated in 0.02939 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top