String font = "asdasd";
Graphics g;
Font fontOfChart = Font.decode(null);
FontMetrics fm = g.getFontMetrics(fontOfChart);
int fontWidth = fm.stringWidth(font);
Graphics g;
Font fontOfChart = Font.decode(null);
FontMetrics fm = g.getFontMetrics(fontOfChart);
int fontWidth = fm.stringWidth(font);
用BufferedImage来做背景
把字体的颜色设置为如红色
写好后对图像像素点进行从左到右逐列的扫描
取出这一列上所有点的颜色,如果都为背景色则继续
一直扫到有非背景色的为止,记下(此为字的起点)坐标
然后倒过来从右到左扫描
同理得到最右的点,记下坐标
把两个横坐标减减应该是最精确的长度了
可惜这方法只比较适用于只有一句话的情况
(精确可惜不标准)
哈哈,一个馊主意,仅供参考。
//*********************g.drawString(str,********);
FontMetrics metrics = g.getFontMetrics();
int length = metrics.stringWidth(str);
for further studies, look at the FontMetrics class API documentation.