分类: Java
2010-02-26 15:31:04
计算字符串的像素长度与高度:
Graphics2D g = (Graphics2D)Toolkit.getDefaultToolkit(). getImage("imgname").getGraphics(); // 设置大字体 Font font = new Font("楷体", Font.ITALIC | Font.BOLD, 72); g.setFont(font); FontRenderContext context = g.getFontRenderContext(); // 获取字体的像素范围对象 Rectangle2D stringBounds = font.getStringBounds("text", context); double fontWidth = stringBounds.getWidth();