我目前正在为我的Java游戏设计菜单系统,我想知道如何让Graphics.drawString()
中的文本居中,这样如果我想要绘制一个文本,它的中心点在X: 50
和Y: 50
,而文本的宽度为30
像素,高度为10
像素,则文本将从X: 35
和Y: 45
处开始。
我能在绘制前确定文本的宽度吗?
那么这就是简单的数学问题。
编辑:我也想知道是否可以获取文本的高度,以便我也可以垂直居中。
任何帮助都将不胜感激!
我目前正在为我的Java游戏设计菜单系统,我想知道如何让Graphics.drawString()
中的文本居中,这样如果我想要绘制一个文本,它的中心点在X: 50
和Y: 50
,而文本的宽度为30
像素,高度为10
像素,则文本将从X: 35
和Y: 45
处开始。
我能在绘制前确定文本的宽度吗?
那么这就是简单的数学问题。
编辑:我也想知道是否可以获取文本的高度,以便我也可以垂直居中。
任何帮助都将不胜感激!
我使用了这个问题上的答案。
我使用的代码大致如下:
/**
* 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);
}
/**
* 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);
}
var text = "trying something";
graphics.setFont(font);
var textWidth = graphics.getFontMetrics().stringWidth(text);
var horizontalPosition = ((yourBufferedImage.getWidth())/2) - (textWidth/2);
var verticalPosition = yourBufferedImage.getHeight()/2;
graphics.drawString(text, horizontalPosition, verticalPosition);
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);
FontMetrics.getHeight() / 2
并不能给我文本“真实”高度的一半... @richj 我已经完成了大部分工作,所以我认为我不会转向JavaFX。那将是另一个游戏的事情。 - Daniel Kvist