点击(此处)折叠或打开

  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.     }

10-18 06:51