【问题标题】:How can I center Graphics.drawString() in Java?如何在 Java 中居中 Graphics.drawString()?
【发布时间】:2015-02-26 15:53:33
【问题描述】:

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

我可以在绘制之前确定文本的宽度吗?
那么数学就很简单了。

编辑:我也想知道是否可以获得文本的高度,以便我也可以垂直居中。

感谢任何帮助!

【问题讨论】:

  • 看起来像这样的副本:stackoverflow.com/questions/258486/…
  • 这是 Swing 让您为之努力的东西。试试这个答案:stackoverflow.com/questions/23729944/… 如果您刚刚开始编写游戏,JavaFX 是 Java 平台中包含的最现代的图形工具包,可能是比 Swing 更好的选择。
  • @mwarren 这部分是重复的,但是有一件事。我想知道如何获得文字的高度,因为FontMetrics.getHeight() / 2 没有给我文字“真实”高度的一半...@richj 我已经做了很多,所以我认为我赢了不要切换到 JavaFX。那将是另一场比赛。
  • fontMetrics.getAscent() 对于您的目的可能会更好一些。它不包括“领先”。

标签: java text draw centering graphics2d


【解决方案1】:

当我必须绘制文本时,我通常需要将文本放在一个边界矩形中。

/**
 * 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);
}

【讨论】:

    【解决方案2】:

    我在this question上使用了答案。

    我使用的代码如下所示:

    /**
     * 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);
    }
    

    【讨论】:

    • 如果图形 g 来自系统,则不应丢弃。
    • 请注意,此方法不使用给定矩形的 x 和 y。相反,它应该是 int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2;和 int y = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
    【解决方案3】:

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

        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 在哪里。

    Graphics2D graphics2DImage = bgImage.createGraphics();
    graphics2DImage.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    
        graphics2DImage.drawImage(bgImage, 0, 0, null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-04
      • 2015-07-15
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多