FontMetrics on uppercase string
Hi, I have a problem when getting the width of a string in my app using the method computeStringWidth() passing in an instance of the FontMetrics class.
Because my string is un uppercase it seems to give me an incorrect value back, looking through the forums it seems this is only the case for uppercase and the method works perfectly with lowercase Strings.
Does anyone have a solution to this problem?
Thanks
Alan
[446 byte] By [
Alan1983a] at [2007-10-2 6:42:33]

Looks OK to me!
import javax.swing.*;
import java.awt.*;
public class Fred2 extends JFrame
{
public Fred2()
{
super("ZZZ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
String test = "hello world";
String test1 = "HELLO WORLD";
FontMetrics fm = g.getFontMetrics();
System.out.println(fm.stringWidth(test) + " " + SwingUtilities.computeStringWidth(fm, test));
System.out.println(fm.stringWidth(test1) + " " + SwingUtilities.computeStringWidth(fm, test1));
int w = fm.stringWidth(test);
g.setColor(Color.blue);
g.drawString(test, 0, 20);
g.setColor(Color.red);
g.drawLine(w,0,w,getHeight());
w = fm.stringWidth(test1);
g.setColor(Color.blue);
g.drawString(test1, 0, 40);
g.setColor(Color.red);
g.drawLine(w,0,w,getHeight());
}
};
panel.setPreferredSize(new Dimension(400,200));
setContentPane(panel);
pack();
}
public static void main(String[] args)
{
new Fred2().setVisible(true);
}
}