【问题标题】:how to do a word wrap on a short string with TextLayout in java Graphics2d?java - 如何在Java Graphics2d中使用TextLayout对短字符串进行自动换行?
【发布时间】:2015-01-14 00:32:45
【问题描述】:

如何在 java 中使用 TextLayout 有效地对下面的短标签字符串进行自动换行?

我的标签只有两三个字长 一些例子: 1、充气温室D10; 2.指挥控制中心A5; 3.杰森鲍里斯;

我想将文字包装成尽可能像正方形,而不是长方形。

所以我的问题是:将建筑物名称包装到第二行而不是一长行需要什么?见下图:

有没有办法设置一行文本中包含的最大字符数,并将剩余的字符换行到第二行等等(需要考虑空格)?

例如,我想将名称“Residential Quarter D12”包装成三行。

  Residential
   Quarter
     D12

并将“Command and Control D16”包装成四行。

 Command 
   and
 Control
   D16

如果 TextLayout 可以像普通的 JLabel 一样理解 html 代码不是很好吗!?然后它会让事情变得简单:

       String label = "<html>" + "Inflatable" + "<br>" + "Greenhouse"  + "<br>" + "D10" + "</html>";

注意:不必每行一个单词。但我想让它们在每一行“居中”

我所拥有的是以下方法,用于生成建筑物名称标签的 BufferedImage 或只是一个人的名字和姓氏。

    private BufferedImage createLabelImage(
    String label, Font font, FontRenderContext fontRenderContext, Color labelColor, 
    Color labelOutlineColor) {

    // Determine bounds.
    TextLayout textLayout1 = new TextLayout(label, font, fontRenderContext);
    Rectangle2D bounds1 = textLayout1.getBounds();

    // Get label shape.
    Shape labelShape = textLayout1.getOutline(null);

    // Create buffered image for label.
    int width = (int) (bounds1.getWidth() + bounds1.getX()) + 4;
    int height = (int) (bounds1.getHeight()) + 4;
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    // Get graphics context from buffered image.
    Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.translate(2D - bounds1.getX(), 2D - bounds1.getY());

    // Draw label outline.
    Stroke saveStroke = g2d.getStroke();
    g2d.setColor(labelOutlineColor);
    g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    g2d.draw(labelShape);
    g2d.setStroke(saveStroke);

    // Fill label
    g2d.setColor(labelColor);
    g2d.fill(labelShape);

    // Dispose of image graphics context.
    g2d.dispose();

    return bufferedImage;
}

如你所见,这个方法只能创建一个只有一行文字的BufferedImage形式的label。

当我在地图上叠加这些 BufferedImage 标签时,它们看起来太长并且彼此重叠。

这就是为什么我需要让每个标签的形状尽可能像正方形。

【问题讨论】:

  • 您是否在问如何创建一个包含居中文本的方形图像,如果将其包裹起来,则每行上的字符数量都尽可能均匀?
  • 是的,你说出了我的想法。我刚刚编辑了我的问题并发布了一张我所拥有的照片。感谢任何提示。

标签: java bufferedimage graphics2d word-wrap


【解决方案1】:

让我尝试提出一个算法。

按空格分割标签得到单词列表,测量每个单词得到数组

int[] wordWidths;
int minWidth=max(wordWidths);
int height=the row height const;
int minHeight=height;
int maxHeight=wordWidths.length*height;

int currentWidth=minWidht;
int currentHeight=maxHeight;

while(currentWidth<currentHeight || wordWidths.length>1) {
  int mergedWidth=find minimal sum of neighbour words' widths
  replace the 2 widths with the mergedWidth reducing the wordWidthssize
  currentHeight=wordWidths.length*height;
}

或者您可以尝试依赖组件。我会定义一个JTextArea 实例,在那里分配标签并尝试使用将宽度减小1 1 并测量宽度的首选高度。 达到最佳尺寸后,您可以致电 theWrappedJtextArea.printAll(g) 将其绘制在您的 BufferedImage's Graphics 上。

【讨论】:

  • 你能进一步分解吗?欣赏!
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
  • 2022-01-18
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多