【问题标题】:java.awt.font letter spacingjava.awt.font 字母间距
【发布时间】:2012-10-25 03:33:25
【问题描述】:

使用 java.awt.font 时是否可以增加字母间距? 像这样的:

Font font = new Font("serif", Font.PLAIN, 21);
//Increase the spacing - perhaps with deriveFont()?

BufferedImage image = new BufferedImage(400, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setFont(font);
graphics.drawString("testing",0,0);

【问题讨论】:

    标签: java fonts awt kerning


    【解决方案1】:

    您可以使用更新的跟踪属性派生新字体:

    Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
    attributes.put(TextAttribute.TRACKING, 0.5);
    Font font2 = font.deriveFont(attributes);
    gr.setFont(font2);
    gr.drawString("testing",0,20);
    

    【讨论】:

      【解决方案2】:

      一个通用的“在宽度为 w 的容器中以当前字体居中此文本”可能是:

          /**
           * Calculates the x-coordinate to use to center the specified message
           * horizontally during a subsequent call to g.drawString().
           * 
           * @param g       - the instance of the Graphics object to use.
           * @param message - the string containing the message to be centered.
           * @param width   - the width of the container to center the message in.
           * @return - the integer x-coordinate to use in a subsequent call to
           *             g.drawString()
           */
          private int centerMessageHorizontal(Graphics g, String message, int width)
          {
              FontMetrics fontMetrics = g.getFontMetrics();
              int textWidth = fontMetrics.stringWidth(message);
              return width / 2 - textWidth / 2;
          }
      

      【讨论】:

      • 这与问题无关
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 2011-07-27
      • 2011-12-11
      相关资源
      最近更新 更多