如何在Java小程序中将文本放置在一个圆内,以使文本的中间部分位于中心?

huangapple go评论95阅读模式
英文:

How to place text inside a circle in java applet such that middle of the text comes at the center

问题

我试图编写一个代码将文本放置在圆的中间使字符串的中心位于圆的中心但是如果字符串的字体大小和圆的直径都很大则文本似乎从中心开始这是我的代码

```java
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;

// 扩展Applet类是必要的
public class Main extends Applet {
    public void paint(Graphics g) {
        g.setColor(Color.yellow);
        int diameter = 500;
        int xpos = 100, ypos = 100;
        g.fillOval(xpos, ypos, diameter, diameter);
        Font f1 = new Font("Arial", Font.BOLD, 24);
        g.setColor(Color.black);
        g.setFont(f1);
        String s = "Text inside a circle";
        g.drawString(s, xpos + (diameter / 2) - (s.length() / 2), ypos + (diameter / 2));
    }
}

这是我得到的输出:

如何在Java小程序中将文本放置在一个圆内,以使文本的中间部分位于中心?

但我希望文本位于中间。


<details>
<summary>英文:</summary>

I&#39;m trying to write a code to place a text in the middle of a circle such that the center of the string is on the center of the circle. But the text appears to start from the center , if the font size of the string and the diameter of the circle are large. 
Here is my code,

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
//extending Applet class is necessary
public class Main extends Applet{
public void paint(Graphics g)
{
g.setColor(Color.yellow);
int diameter=500;
int xpos=100,ypos=100;
g.fillOval(xpos,ypos,diameter,diameter);
Font f1 = new Font("Arial",Font.BOLD,24);
g.setColor(Color.black);
g.setFont(f1);
String s="Text inside a circle";
g.drawString(s,xpos+(diameter/2)-(s.length()/2),ypos+(diameter/2));

}

}

This is the output I&#39;m getting:

[![https://i.stack.imgur.com/ZebQT.png][1]][1]

But I want text to be in the middle.


  [1]: https://i.stack.imgur.com/ZebQT.png

</details>


# 答案1
**得分**: 1


> 但是文本似乎是从中心开始的

String变量的长度只返回字符串中字符的数量,而不是字符串的宽度(以像素为单位)。

您需要知道文本在像素中的宽度,以便将其居中显示在圆内。为此,您可以使用`FontMetrics`类。

```java
FontMetrics fm = g.getFontMetrics();
int width = fm.stringWidth(s);
int offset = (diameter - width ) / 2;
g.drawString(s, xpos + offset, ypos + (diameter / 2));
//g.drawString(s,xpos+(diameter/2)-(s.length()/2),ypos+(diameter/2));

请注意,上述代码仅根据宽度进行居中。您还应该根据高度进行居中。为此,您可以使用getStringBounds(...)方法。这将允许您更好地近似垂直居中。该方法将返回一个矩形,因此您可以将矩形的宽度和高度用于水平和垂直居中。

英文:

> But the text appears to start from the center

The length of the String variable only returns the number of characters in the String, not the width (in pixels) of the String.

You need to know the width of the text in pixels so it can be centered in the circle. For this you use the FontMetrics class.

FontMetrics fm = g.getFontMetrics();
int width = fm.stringWidth(s);
int offset = (diameter - width ) / 2;
g.drawString(s, xpos + offset, ypos + (diameter / 2));
//g.drawString(s,xpos+(diameter/2)-(s.length()/2),ypos+(diameter/2));

Note the above will only center based on the width. You should also center based on the height. For this you can use the getStringBounds(...) method. This will allow you to better approximated the vertical centering as well. This will return a Rectangle so you could use the width and height of the Rectangle for both the horizontal and vertical centering.

答案2

得分: 0

你将字符串输出从圆心向左移动,但移动的值太小。

-(s.length()/2 使用字符串长度(以字符为单位)。您可以将此值乘以与字体大小有关的系数(在您的情况下为24),但不同的字符具有不同的宽度("Arial" 不是等宽字体)。

如果您需要更精确的定位,请考虑计算字符串在像素中的宽度。也许可以使用 getFontMetrics + stringWidth 方法。这里有一个随机找到的示例

英文:

You shift string output to the left from circle center, but shift value is too small

-(s.length()/2

uses string length in chars. You can multiply this value by coefficient depending on font size (24 in your case), but different chars have different width ("Arial" is not fixed width font).

If you need more exact positioning, consider calculation of string width in pixels. Perhaps getFontMetrics + stringWidth. Arbitrary found example

huangapple
  • 本文由 发表于 2020年10月27日 17:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64551316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定