Java Swing Font Metrics wrapString(String s, FontMetrics fm, int width)

Here you can find the source of wrapString(String s, FontMetrics fm, int width)

Description

wrap String

License

Open Source License

Declaration

private static List<String> wrapString(String s, FontMetrics fm, int width) 

Method Source Code

//package com.java2s;
/*/*from   w w w . j a  v a  2s.  c  om*/
 * ?Copyright (C) 2013 Atlas of Living Australia
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 */

import java.awt.FontMetrics;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

public class Main {
    private static List<String> wrapString(String s, FontMetrics fm, int width) {
        List<String> lines = new ArrayList<String>();
        StringBuilder line = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            String test = line.toString() + ch;
            if (fm.stringWidth(test) > width) {
                if (test.length() > 1) {
                    // Backtrack to look for a space...
                    boolean breakFound = false;
                    if (ch != ' ') {

                        for (int j = line.length() - 1; j > 0; --j) {
                            if (line.charAt(j) == ' ') {
                                lines.add(line.substring(0, j));
                                line = new StringBuilder(line.substring(j + 1));
                                breakFound = true;
                                break;
                            }
                        }
                    }
                    if (!breakFound) {
                        lines.add(line.toString());
                        line = new StringBuilder();
                    }
                    line.append(ch);
                } else {
                    lines.add(test);
                    line = new StringBuilder();
                }
            } else {
                line.append(ch);
            }
        }
        if (line.length() > 0) {
            lines.add(line.toString());
        }
        return lines;
    }

    /**
     * Returns the width of the passed in String.
     *
     * @param c
     *            JComponent that will display the string, may be null
     * @param fm
     *            FontMetrics used to measure the String width
     * @param string
     *            String to get the width of
     */
    public static int stringWidth(JComponent c, FontMetrics fm, String string) {
        return fm.stringWidth(string);
    }
}

Related

  1. getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
  2. getTextBounds(FontMetrics fm, String s)
  3. getWidthOfText(FontMetrics fontMetrics, String text)
  4. layoutMultilineCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap, int minLines, int maxLines)
  5. stringWidth(JComponent c, FontMetrics fm, String string)
  6. wrapText(String text, FontMetrics metrics, int maxWidth)