GraphicsUtils { public static Shape getOutline(String text, Font font, float wrappingWidth, double alignment) { boolean wordWrap = true; if (wrappingWidth <= 0f) { wordWrap = false; wrappingWidth = Float.MAX_VALUE; } AttributedString string = new AttributedString(text); string.addAttribute(TextAttribute.FONT, font); AttributedCharacterIterator iterator = string.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc); List<TextLayout> lines = new LinkedList<>(); while (measurer.getPosition() < text.length()) { int nextBreakPos = measurer.nextOffset(wrappingWidth); int lineBreakPos = text.indexOf('\n', measurer.getPosition()) + 1; int breakPos = nextBreakPos; if (lineBreakPos > 0 && lineBreakPos < nextBreakPos) { breakPos = lineBreakPos; } TextLayout line = measurer.nextLayout(wrappingWidth, breakPos, false); lines.add(line); } if (!wordWrap) { float advanceMax = 0f; for (TextLayout line : lines) { advanceMax = Math.max(line.getAdvance(), advanceMax); } wrappingWidth = advanceMax; } AffineTransform txLinePos = new AffineTransform(); Area outlineAllLines = null; for (TextLayout line : lines) { double dx = alignment*(wrappingWidth - line.getAdvance()); txLinePos.translate(dx, line.getAscent()); Area outlineLine = new Area(line.getOutline(txLinePos)); if (outlineAllLines == null) { outlineAllLines = outlineLine; } else { outlineAllLines.add(outlineLine); } txLinePos.translate(-dx, line.getDescent() + line.getLeading()); } return outlineAllLines; } protected  GraphicsUtils(); static Shape getOutline(String text, Font font, float wrappingWidth,
			double alignment); static void fillPaintedShape(Graphics2D graphics, Shape shape,
			Paint paint, Rectangle2D paintBounds); static void drawPaintedShape(Graphics2D graphics, Shape shape,
			Paint paint, Rectangle2D paintBounds, Stroke stroke); static double[] rgb2xyz(double[] rgb, double[] xyz); static double[] luv2xyz(double[] luv, double[] xyz); static double[] xyz2rgb(double[] xyz, double[] rgb); static double[] xyz2luv(double[] xyz, double[] luv); static double[] rgb2luv(double[] rgb, double[] luv); static double[] luv2rgb(double[] luv, double[] rgb); static Color blend(Color color1, Color color2, double weight); static Color deriveWithAlpha(Color color, int alpha); static Color deriveDarker(Color color); static Color deriveBrighter(Color color);  }
@Test public void testGetOutline() { Shape outline = GraphicsUtils.getOutline( "foobar", Font.decode(null), 0f, 0.5); assertNotNull(outline); Rectangle2D bounds = outline.getBounds2D(); assertTrue(bounds.getWidth() > 0.0); assertTrue(bounds.getHeight() > 0.0); }