How can I center Graphics.drawString() in Java?

前端 未结 3 2238
忘了有多久
忘了有多久 2020-12-14 00:45

I\'m currently working on the menu system for my Java game, and I wonder how I can center the text from Graphics.drawString(), so that if I want to dra

相关标签:
3条回答
  • 2020-12-14 01:22

    When I have to draw text, I usually need to center the text in a bounding rectangle.

    /**
     * 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);
    }
    
    0 讨论(0)
  • 2020-12-14 01:30

    I did create a function to keep the text in center of the image

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

    where, graphics2DImage is.

    Graphics2D graphics2DImage = bgImage.createGraphics();
    graphics2DImage.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    
        graphics2DImage.drawImage(bgImage, 0, 0, null);
    
    0 讨论(0)
  • 2020-12-14 01:31

    I used the answer on this question.

    The code I used looks something like this:

    /**
     * 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);
    }
    
    0 讨论(0)
提交回复
热议问题