使用 Java Graphics.drawString 替换的完全理由?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/400566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:13:48  来源:igfitidea点击:

Full-justification with a Java Graphics.drawString replacement?

javagraphicstext2d

提问by David Koelle

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

有谁知道可以让您在 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?

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

回答by coobird

Although not the most elegant nor robust solution, here's an method that will take the Fontof the current Graphicsobject and obtain its FontMetricsin order to find out where to draw the text, and if necessary, move to a new line:

虽然不是最优雅、最健壮的解决方案,但这里有一个方法,它将获取Font当前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;
    }
}

This implementation will separate the given Stringinto an array of Stringby using the splitmethod 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 curXposition.

此实现将通过使用以空格字符作为唯一单词分隔符的方法将给定String的数组分隔为一个数组,因此它可能不是很健壮。它还假定单词后跟一个空格字符并在移动位置时相应地进行操作。StringsplitcurX

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 FontMetricsclass.

如果我是你,我不会推荐使用这个实现,但可能为了进行另一个实现所需的函数仍然会使用FontMetricsclass提供的方法。

回答by PhiLho

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...

对于自动换行,您可能对How to output a String on multiple lines using Graphics感兴趣。这里没有任何理由,不确定是否容易(或不可能!)添加...