AbstractAWTDrawVisitor implements IDrawVisitor { protected Rectangle2D getTextBounds(String text, double xCoord, double yCoord, Graphics2D graphics) { FontMetrics fontMetrics = graphics.getFontMetrics(); Rectangle2D bounds = fontMetrics.getStringBounds(text, graphics); double widthPad = 3; double heightPad = 1; double width = bounds.getWidth() + widthPad; double height = bounds.getHeight() + heightPad; int[] point = this.transformPoint(xCoord, yCoord); return new Rectangle2D.Double(point[0] - width / 2, point[1] - height / 2, width, height); }  int[] transformPoint(double xCoord, double yCoord); void transformPoint(double[] xy); @Override void setTransform(AffineTransform transform);  }
@Test public void testGetTextBounds() { AbstractAWTDrawVisitor visitor = new NestedAWTDrawVisitor(); visitor.setTransform(new AffineTransform()); Image image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) image.getGraphics(); Rectangle2D rectangle = visitor.getTextBounds("Foo", 3, 5, g2d); Assert.assertNotNull(rectangle); }