void (@NotNull Graphics2D g, @NotNull Rectangle rect, @NotNull String str, boolean horzCentered, boolean vertCentered) { FontMetrics fm = g.getFontMetrics(g.getFont()); int textWidth = fm.stringWidth(str) - 1; int x = horzCentered ? Math.max(rect.x, rect.x + (rect.width - textWidth) / 2) : rect.x; int y = vertCentered ? Math.max(rect.y, rect.y + rect.height / 2 + fm.getAscent() * 2 / 5) : rect.y; Shape oldClip = g.getClip(); g.clip(rect); g.drawString(str, x, y); g.setClip(oldClip); }