Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

getting the width and height of a String to draw a rectangle around it

Thread view: 
jakester - 05 Apr 2007 00:52 GMT
I have to draw (using Graphics or Graphics2D) a rectangle around a
String. How do I get the width and height of a String to draw a
rectangle around it? It seems there are many factors (i.e. Font/Face).
A simple example would help me.

Thanks.
Daniel Pitts - 05 Apr 2007 01:12 GMT
> I have to draw (using Graphics or Graphics2D) a rectangle around a
> String. How do I get the width and height of a String to draw a
> rectangle around it? It seems there are many factors (i.e. Font/Face).
> A simple example would help me.
>
> Thanks.

if you have a Graphics object g.
   g.getFontMetrics().getStringBounds();
loop up: java.awt.FontMetrics.

Alternatively:

   JLabel label = new JLabel("My Text");
   label.setBorder(BorderFactory.createLineBorder(Color.black));
Esmond Pitt - 05 Apr 2007 01:37 GMT
> I have to draw (using Graphics or Graphics2D) a rectangle around a
> String. How do I get the width and height of a String to draw a
> rectangle around it? It seems there are many factors (i.e. Font/Face).
> A simple example would help me.

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#getStringBounds(char[
],%20int,%20int,%20java.awt.font.FontRenderContext
)
or one of the overloads of
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#getLineMetrics(java.t
ext.CharacterIterator,%20int,%20int,%20java.awt.font.FontRenderContext
)
Daniel Pitts - 05 Apr 2007 05:57 GMT
> > I have to draw (using Graphics or Graphics2D) a rectangle around a
> > String. How do I get the width and height of a String to draw a
[quoted text clipped - 3 lines]
> http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#getStringB...[],%20in
t,%20int,%20java.awt.font.FontRenderContext
)
> or one of the overloads ofhttp://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#getLineMet...)

Funny, he gets a reply from a Pitts and a Pitt.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.