题
我想中心,一个“”系统”,Font.BOLD,在Java 90" 字体绘制的字符串。我试过。(宽/ 2 - (font.size / 2 * num_of_chars)),但没有奏效。
g2d.setFont(new Font("system", Font.BOLD, 90));
g2d.drawString("Pause", (int) ((800/2) - ((Font.getSize()/2) * 5)),270);
解决方案
使用 getFontMetrics()
一> getStringBounds(String, Graphics)
得到的边界串与当前字体。
因此,这将是这个样子:
g2d.setFont(new Font("system", Font.BOLD, 90));
String msg = "Pause";
Rectangle2D bounds = g2d.getFontMetrics().getStringBounds(msg, g2d);
g2d.drawString(msg, (int) ((getWidth() + bounds.getWidth()) / 2), 270);
不隶属于 StackOverflow