首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使Graphics.drawString()在Java中居中?

如何使Graphics.drawString()在Java中居中?
EN

Stack Overflow用户
提问于 2014-12-30 13:15:40
回答 4查看 65.2K关注 0票数 41

我目前正在为我的Java游戏开发菜单系统,我想知道如何用Graphics.drawString()对文本进行居中,这样如果我想要画一个以X: 50Y: 50为中心,文本是30像素宽和10像素高的文本,文本将从X: 35Y: 45开始。

在绘制文本之前,我可以确定它的宽度吗?

那就是简单的数学了。

编辑:--我也想知道我是否能得到文本的高度,这样我也可以垂直地对齐它。

任何帮助都是非常感谢的!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-01-02 10:00:49

我在this question上使用了答案。

我使用的代码如下所示:

代码语言:javascript
运行
复制
/**
 * Draw a String centered in the middle of a Rectangle.
 *
 * @param g The Graphics instance.
 * @param text The String to draw.
 * @param rect The Rectangle to center the text in.
 */
public void drawCenteredString(Graphics g, String text, Rectangle rect, Font font) {
    // Get the FontMetrics
    FontMetrics metrics = g.getFontMetrics(font);
    // Determine the X coordinate for the text
    int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2;
    // Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
    int y = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
    // Set the font
    g.setFont(font);
    // Draw the String
    g.drawString(text, x, y);
}
票数 93
EN

Stack Overflow用户

发布于 2014-12-30 19:08:49

当我必须绘制文本时,我通常需要在一个边框中对文本进行居中。

代码语言:javascript
运行
复制
/**
 * This method centers a <code>String</code> in 
 * a bounding <code>Rectangle</code>.
 * @param g - The <code>Graphics</code> instance.
 * @param r - The bounding <code>Rectangle</code>.
 * @param s - The <code>String</code> to center in the
 * bounding rectangle.
 * @param font - The display font of the <code>String</code>
 * 
 * @see java.awt.Graphics
 * @see java.awt.Rectangle
 * @see java.lang.String
 */
public void centerString(Graphics g, Rectangle r, String s, 
        Font font) {
    FontRenderContext frc = 
            new FontRenderContext(null, true, true);

    Rectangle2D r2D = font.getStringBounds(s, frc);
    int rWidth = (int) Math.round(r2D.getWidth());
    int rHeight = (int) Math.round(r2D.getHeight());
    int rX = (int) Math.round(r2D.getX());
    int rY = (int) Math.round(r2D.getY());

    int a = (r.width / 2) - (rWidth / 2) - rX;
    int b = (r.height / 2) - (rHeight / 2) - rY;

    g.setFont(font);
    g.drawString(s, r.x + a, r.y + b);
}
票数 8
EN

Stack Overflow用户

发布于 2020-06-15 09:43:55

我确实创建了一个函数来将文本保持在图像的中心。

代码语言:javascript
运行
复制
    private static void setTextCenter(Graphics2D graphics2DImage, String string,
                                        BufferedImage bgImage) {
    int stringWidthLength = (int)
            graphics2DImage.getFontMetrics().getStringBounds(string, graphics2DImage).getWidth();
    int stringHeightLength = (int)
            graphics2DImage.getFontMetrics().getStringBounds(string, graphics2DImage).getHeight();

    int horizontalCenter = bgImage.getWidth() / 2 - stringWidthLength / 2;
    int verticalCenter = bgImage.getHeight() / 2 - stringHeightLength / 2;
    graphics2DImage.drawString(string, horizontalCenter, verticalCenter);
}

在哪里,graphics2DImage在哪里。

代码语言:javascript
运行
复制
Graphics2D graphics2DImage = bgImage.createGraphics();
graphics2DImage.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    graphics2DImage.drawImage(bgImage, 0, 0, null);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27706197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档