11

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);
4

2 に答える 2

19

更新された追跡属性を使用して、新しいフォントを派生させることができます。

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);
于 2012-11-05T12:33:45.720 に答える
0

一般的な「幅 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;
    }
于 2021-03-12T17:03:07.843 に答える