Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134593
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-15 23:00
文章分类

全部博文(46)

文章存档

2018年(18)

2017年(11)

2015年(14)

2014年(3)

我的朋友

分类: Java

2018-01-16 23:44:57


点击(此处)折叠或打开

  1. /**
  2.      * 获取对应字体的文字的高度
  3.      *
  4.      * @param g2d
  5.      * @param font
  6.      * @return
  7.      * @parm
  8.      * @exception
  9.      */
  10.     public double getFontHeight(Graphics2D g2d, Font font) {
  11.         // 设置大字体
  12.         FontRenderContext context = g2d.getFontRenderContext();
  13.         // 获取字体的像素范围对象
  14.         Rectangle2D stringBounds = font.getStringBounds("w", context);
  15.         double fontWidth = stringBounds.getWidth();
  16.         return fontWidth;
  17.     }
  18.   
  19.     /**
  20.      * 获取对应的文字所占有的长度
  21.      *
  22.      * @param g2d
  23.      * @param font
  24.      * @return
  25.      * @parm
  26.      * @exception
  27.      */
  28.     public double getFontSize(Graphics2D g2d, Font font, String text) {
  29.         // 设置大字体
  30.         FontRenderContext context = g2d.getFontRenderContext();
  31.         // 获取字体的像素范围对象
  32.         Rectangle2D stringBounds = font.getStringBounds(text, context);
  33.         double fontWidth = stringBounds.getWidth();
  34.         return fontWidth;
  35.     }

阅读(1422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~