code: protected Rectangle2D getTextBounds(String text, Graphics2D g2) {
          return new TextLayout(text, g2.getFont(), g2.getFontRenderContext()).getBounds();
      }
docs: Obtain the exact bounding box of the {@code text} in the provided
  graphics environment.
  
  @param text the text to obtain the bounds of
  @param g2   the graphic environment
  @return bounds of the text
  @see TextLayout