code: 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);
      }
docs: Calculates the boundaries of a text string in screen coordinates.
  
  @param text     the text string
  @param xCoord   the world x-coordinate of where the text should be placed
  @param yCoord   the world y-coordinate of where the text should be placed
  @param graphics the graphics to which the text is provided as output
  @return         the screen coordinates