用 Java Graphics.drawString 替换的充分理由?

发布于 2024-07-12 02:18:25 字数 171 浏览 3 评论 0 原文

有谁知道现有的代码可以让您在 Java2D 中绘制完全合理的文本吗?

例如,如果我说,drawString("sample text here", x, y, width),是否有一个现有的库可以计算出该文本有多少适合宽度,做一些字符间距使文本看起来不错,并自动进行基本的自动换行?

Does anyone know of existing code that lets you draw fully justified text in Java2D?

For example, if I said, drawString("sample text here", x, y, width), is there an existing library that could figure out how much of that text fits within the width, do some inter-character spacing to make the text look good, and automatically do basic word wrapping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-07-19 02:18:25

虽然不是最优雅也不是最强大的解决方案,但这里有一个方法,它将采用 当前 字体 ="noreferrer">Graphics 对象并获取其 FontMetrics 以便找出在哪里绘制文本,并在必要时移动到新行:

public void drawString(Graphics g, String s, int x, int y, int width)
{
    // FontMetrics gives us information about the width,
    // height, etc. of the current Graphics object's Font.
    FontMetrics fm = g.getFontMetrics();

    int lineHeight = fm.getHeight();

    int curX = x;
    int curY = y;

    String[] words = s.split(" ");

    for (String word : words)
    {
        // Find out thw width of the word.
        int wordWidth = fm.stringWidth(word + " ");

        // If text exceeds the width, then move to next line.
        if (curX + wordWidth >= x + width)
        {
            curY += lineHeight;
            curX = x;
        }

        g.drawString(word, curX, curY);

        // Move over to the right for next word.
        curX += wordWidth;
    }
}

此实现将分隔给定的 使用 split 方法使用空格字符作为唯一的单词分隔符,因此它可能不是很健壮。 它还假设该单词后跟一个空格字符,并在移动 curX 位置时采取相应的操作。

如果我是您,我不会建议使用此实现,但可能为了进行另一个实现而需要的功能仍将使用 FontMetrics

Although not the most elegant nor robust solution, here's an method that will take the Font of the current Graphics object and obtain its FontMetrics in order to find out where to draw the text, and if necessary, move to a new line:

public void drawString(Graphics g, String s, int x, int y, int width)
{
    // FontMetrics gives us information about the width,
    // height, etc. of the current Graphics object's Font.
    FontMetrics fm = g.getFontMetrics();

    int lineHeight = fm.getHeight();

    int curX = x;
    int curY = y;

    String[] words = s.split(" ");

    for (String word : words)
    {
        // Find out thw width of the word.
        int wordWidth = fm.stringWidth(word + " ");

        // If text exceeds the width, then move to next line.
        if (curX + wordWidth >= x + width)
        {
            curY += lineHeight;
            curX = x;
        }

        g.drawString(word, curX, curY);

        // Move over to the right for next word.
        curX += wordWidth;
    }
}

This implementation will separate the given String into an array of String by using the split method with a space character as the only word separator, so it's probably not very robust. It also assumes that the word is followed by a space character and acts accordingly when moving the curX position.

I wouldn't recommend using this implementation if I were you, but probably the functions that are needed in order to make another implementation would still use the methods provided by the FontMetrics class.

向地狱狂奔 2024-07-19 02:18:25

对于自动换行,您可能会对 如何使用图形在多行上输出字符串。 这里没有理由,不确定添加是否容易(或不可能!)...

For word wrapping, you might be interested by How to output a String on multiple lines using Graphics. No justification here, not sure if it is easy (or impossible!) to add...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文