FONT CONTROL

IN PREPERATION!




1. FONT METRICS

Text is positioned according to its start coordinates. Sometimes it is nice to center text. To do this we need information about the width of out string. THe class FontMetrics contains such information. The code fragment given below posuitions a givem string (s1) so that it is centered at x. The code makes use of the stringWidth instance method from theFontMetrics class to determine the width of a given string s1.

g.setColor(Color.black);
Font mono = new Font("Monospaced",Font.BOLD,10);
g.setFont(mono);
FontMetrics newFM = g.getFontMetrics(mono);
int xLength = newFM.stringWidth(s1);
g.drawString(s1,(x-(xLength/2)),y);



Created and maintained by Frans Coenen. Last updated 30 June 2003