英文:
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));
}
}
这是我得到的输出:
但我希望文本位于中间。
<details>
<summary>英文:</summary>
I'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'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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论