List of utility methods to do FontMetrics
String | stringByTruncatingToFitInWidth(String s, int width, Graphics g, String truncatedSuffix) Truncate a string until it fits in the given width. if (s == null) return null; if (truncatedSuffix == null) truncatedSuffix = ""; int length = s.length(); String truncated = s; while (length > 0) { Rectangle2D r = g.getFontMetrics().getStringBounds(truncated, g); ... |
Dimension | stringDimension(Graphics g, Font f, String s) Returns the dimension of the specified string s in specified graphic and font environment. FontMetrics fm = g.getFontMetrics(f); return new Dimension(fm.stringWidth(s), fm.getHeight()); |
Dimension | StringDimension(Graphics g, String text) String Dimension FontMetrics metrics = g.getFontMetrics(g.getFont()); int hgt = metrics.getHeight(); int adv = metrics.stringWidth(text); return new Dimension(adv, hgt); |
String[] | wordWrap(String text, FontMetrics metrics, double width) Returns the specified text in lines that fit within the specified width when the specified font metrics are applied to the text List<String> result = new ArrayList<String>(); String[] lines = text.split("\n"); for (int i = 0; i < lines.length; i++) { int lineWidth = 0; int charCount = 0; StringBuilder currentLine = new StringBuilder(); String[] words = lines[i].split("\\s+"); Stack<String> wordStack = new Stack<String>(); ... |
List | wrapText(String text, int width, FontMetrics fontMetrics) wrap Text LinkedList<String> lines = new LinkedList<String>(); if (isEmpty(text)) { lines.add(text); return lines; text = removePostfixedNewline(text); if (!text.contains("\n") && fontMetrics.stringWidth(text) <= width) { lines.add(text); ... |