Article View: borland.public.jbuilder.java.api
Article #1720Re: character spacing
From: Jeff
Date: Mon, 15 Oct 2001 18:42
Date: Mon, 15 Oct 2001 18:42
40 lines
2332 bytes
2332 bytes
It works, but it doesn't look as good. There's a perceptible difference
in the spacing of some letter combinations. I made a couple of changes
to make the spacing uniform, regardless of the pitch:
public void paintComponent(Graphics g) {
// Define font used to draw text.
Font f = new Font("SansSerif", Font.PLAIN, 12);
g.setFont(f);
// Calculate baseline origin so text is centered vertically.
FontMetrics fm = g.getFontMetrics(f);
int originY = getHeight()/2 + fm.getHeight()/2 - fm.getDescent();
// Print text with padding between each character.
char [] textChars = this.text.toCharArray();
int originX = 0;
for (int i = 0; i < this.text.length(); i++) {
g.drawChars(textChars, i, 1, originX, originY);
originX += fm.charWidth(textChars[i])+ this.offset;
}
}
This code has the added benefit of not offsetting the first character.
The program I needed this for needs a fixed pitch, but I'm glad you
raised the question, because I like the revised code better. I'm new to
Java and really appreciate the feedback.
- Jeff
"Rich Wilkman" <richw@formlessvoid.com> wrote in message
news:3BCB1A05.4CD709D2@formlessvoid.com...
> Jeff:
>
> Interesting! How well does it work between serif and sans-serif fixed
> fonts?
>
> -Rich
>
[snip]
Technical Details
Message-ID:
<3bcb2e17_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed1.earthlink.net!newsfeed2.earthlink.net!newsfeed.earthlink.net!lsanca1-snf1!news.gtei.net!enterprise!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsReferences:
<3bc649f7$1_2@dnews> <3bc99a4b$1_1@dnews> <3BCB1A05.4CD709D2@formlessvoid.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,a2b03a6d9808c012 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-10-15 11:51:08 PST Reply-To: "Jeff" <mevyap@hotmail.com> From: "Jeff" <mevyap@hotmail.com> Newsgroups: borland.public.jbuilder.java.api References: <3bc649f7$1_2@dnews> <3bc99a4b$1_1@dnews> <3BCB1A05.4CD709D2@formlessvoid.com> Subject: Re: character spacing Date: Mon, 15 Oct 2001 14:42:30 -0400 Lines: 42 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 204.249.220.101 Message-ID: <3bcb2e17_1@dnews> X-Trace: dnews 1003171351 204.249.220.101 (15 Oct 2001 11:42:31 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed1.earthlink.net!newsfeed2.earthlink.net!newsfeed.earthlink.net!lsanca1-snf1!news.gtei.net!enterprise!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!dnews