Article View: borland.public.jbuilder.java.api
Article #1072Re: How calculate the length in pixels of a String
From: Jeff Heaton
Date: Sun, 02 Jun 2002 02:38
Date: Sun, 02 Jun 2002 02:38
39 lines
2334 bytes
2334 bytes
Here is a short example program that shows how to center that I used
for one of my Java classes:
http://www.jeffheaton.com/java/intro/Class6Example.shtml
But basically you just need to do this..
Font f = new Font(...)
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth("Your string here");
Jeff Heaton
http://www.jeffheaton.com
On Fri, 31 May 2002 14:16:12 +0200, "Antonio"
<avazquez@winsoftsystems.com> wrote:
>Suppose the following sentences:
>
>String myText = new String("Hello world!");
>Font smallFont = new Font("SansSerif", Font.PLAIN, 6);
> .. ..
> .. ..
> pageWidth = getXXX <<<<<<<<<<<<(retrieve page width)
> .. ..
> g2d.setFont(smallFont);
> g2d.drawString(myText,pen.x,,pen.y);
>
>
>How can I get the pixels used by my text to center the line, because i don't
>know the pixel used by drawString to get a line like...
>
> g2d.drawString(myText, pen.x + ((pageWidth -
>pixels_used_by_myText) / 2), pen.y)
>
>In windows 32 there are several API's to get the FontMetric....
>
>I will thank any piece of code
Technical Details
Message-ID:
<e71jfug8h0023sjopkdraod2mqaugcbdbd@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!news-xfer.siscom.net!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed-east.nntpserver.com!nntpserver.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!f2df5386!not-for-mailReferences:
<3cf768fb_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,15edc31994bc18d7 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-06-01 19:38:37 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!news-xfer.siscom.net!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed-east.nntpserver.com!nntpserver.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!f2df5386!not-for-mail From: Jeff Heaton <heatonjNO_SPAMMERZ@heat-on.com> Newsgroups: borland.public.jbuilder.java.api Subject: Re: How calculate the length in pixels of a String Message-ID: <e71jfug8h0023sjopkdraod2mqaugcbdbd@4ax.com> References: <3cf768fb_2@dnews> X-Newsreader: Forte Agent 1.9/32.560 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 40 Date: Sun, 02 Jun 2002 02:38:32 GMT NNTP-Posting-Host: 24.217.161.204 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 1022985512 24.217.161.204 (Sat, 01 Jun 2002 19:38:32 PDT) NNTP-Posting-Date: Sat, 01 Jun 2002 19:38:32 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net