This code was written to test font on different system. Lucida is a physical fonts that comes with every JDK. You can find it in jre/lib/font folder. I've tested it on three systems. They all gave me different results. Can anybody tell me why? How can I make font system independent?? (the 3 systems are window, alpha, and sun) Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Metrics extends JFrame {
public Metrics()
{
super( "Demonstrating FontMetrics" );
setSize( 510, 500 );
show();
}
public void paint( Graphics g )
{
int h = 40;
g.setFont( new Font( "Lucida Sans", Font.PLAIN, 9 ) );
FontMetrics fm = g.getFontMetrics();
g.drawString( "Current font: " + g.getFont(), 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "charWidth: " + fm.charWidth('A'), 10, h );
h += 15;
String str = "123456789 123456789 123456789 123456789 123456789 ";
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
double b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
h += 30;
Font font = new Font( "Lucida Bright", Font.PLAIN, 9 );
fm = g.getFontMetrics( font );
g.setFont( font );
g.drawString( "Current font: " + font, 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "Width: " + fm.charWidth('A'), 10, h );
h += 15;
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
h += 30;
font = new Font( "Lucida Sans Typewriter", Font.PLAIN, 9 );
fm = g.getFontMetrics( font );
g.setFont( font );
g.drawString( "Current font: " + font, 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "Width: " + fm.charWidth('A'), 10, h );
h += 15;
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
}
public static void main( String args[] )
{
Metrics app = new Metrics();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Metrics extends JFrame {
public Metrics()
{
super( "Demonstrating FontMetrics" );
setSize( 510, 500 );
show();
}
public void paint( Graphics g )
{
int h = 40;
g.setFont( new Font( "Lucida Sans", Font.PLAIN, 9 ) );
FontMetrics fm = g.getFontMetrics();
g.drawString( "Current font: " + g.getFont(), 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "charWidth: " + fm.charWidth('A'), 10, h );
h += 15;
String str = "123456789 123456789 123456789 123456789 123456789 ";
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
double b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
h += 30;
Font font = new Font( "Lucida Bright", Font.PLAIN, 9 );
fm = g.getFontMetrics( font );
g.setFont( font );
g.drawString( "Current font: " + font, 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "Width: " + fm.charWidth('A'), 10, h );
h += 15;
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
h += 30;
font = new Font( "Lucida Sans Typewriter", Font.PLAIN, 9 );
fm = g.getFontMetrics( font );
g.setFont( font );
g.drawString( "Current font: " + font, 10, h );
h += 15;
g.drawString( "Ascent: " + fm.getAscent(), 10, h );
h += 15;
g.drawString( "Descent: " + fm.getDescent(), 10, h );
h += 15;
g.drawString( "Height: " + fm.getHeight(), 10, h );
h += 15;
g.drawString( "Leading: " + fm.getLeading(), 10, h );
h += 15;
g.drawString( "Width: " + fm.charWidth('A'), 10, h );
h += 15;
g.drawString( str + "width:" + fm.stringWidth(str), 10, h );
h += 15;
b = fm.getStringBounds(str, g).getWidth();
g.drawString( "String Bound: " + b, 10, h );
}
public static void main( String args[] )
{
Metrics app = new Metrics();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
Winnt\Fonts\, then try again.