String (String fullText, FontMetrics fm, int maxWidth, boolean isTutorial) { if (isTutorial) { String tutorialTruncated = fullText; while (fm.stringWidth(tutorialTruncated) > maxWidth) { tutorialTruncated = tutorialTruncated.substring(0, tutorialTruncated.length() - 1); } return tutorialTruncated + "..."; } int left = 1; int right = 1; int center = fullText.length() / 2; String s = fullText.substring(0, center - left) + "..." + fullText.substring(center + right); while (fm.stringWidth(s) > maxWidth) { if (left == right) { left++; } else { right++; } if (center - left < 0 || center + right >= fullText.length()) { return ""; } s = fullText.substring(0, center - left) + "..." + fullText.substring(center + right); } return s; }