目录

我的学习分享

记录精彩的程序人生

java 能获取到文字所使用某种字体后的宽度及高度

// 创建图片对象
BufferedImage bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

//得到Graphics2D 对象
Graphics2D g2d = (Graphics2D) bimg.getGraphics();

// 设置字体
Font font = new Font("宋体", Font.BOLD, 30);
g2d.setFont(font);

FontMetrics fm = g2d.getFontMetrics(font);

// 获取文字宽度
int textWidth = fm.stringWidth(text);