Java FontMetrics splitStringWithLength(String s, int length, FontMetrics fm)

Here you can find the source of splitStringWithLength(String s, int length, FontMetrics fm)

Description

This methods will split a String in different lines according to a maximum length.

License

Open Source License

Parameter

Parameter Description
s the string
length the length
fm the font metrics

Return

the formatted line

Declaration

public final static String[] splitStringWithLength(String s, int length, FontMetrics fm) 

Method Source Code

//package com.java2s;
/*******************************************************************************
  * GenPlay, Einstein Genome Analyzer/* ww  w .  j a  v  a 2s  . co m*/
  * Copyright (C) 2009, 2014 Albert Einstein College of Medicine
  * 
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * Authors: Julien Lajugie <julien.lajugie@einstein.yu.edu>
  *          Nicolas Fourel <nicolas.fourel@einstein.yu.edu>
  *          Eric Bouhassira <eric.bouhassira@einstein.yu.edu>
  * 
  * Website: <http://genplay.einstein.yu.edu>
  ******************************************************************************/

import java.awt.FontMetrics;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
      * This methods will split a String in different lines according to a maximum length.
      * It does split in middle of a word but at a whitespace if necessary
      * @param s         the string
      * @param length   the length
      * @param fm      the font metrics
      * @return         the formatted line
      */
    public final static String[] splitStringWithLength(String s, int length, FontMetrics fm) {
        List<String> result = new ArrayList<String>();
        String[] array = split(s, ' ');
        String current = array[0];
        int currentLength = fm.stringWidth(current);

        for (int i = 1; i < array.length; i++) {
            String tmp = array[i];
            currentLength += fm.stringWidth(" " + tmp);
            if (currentLength < length) {
                current += " " + tmp;
            } else {
                result.add(current);
                current = tmp;
                currentLength = fm.stringWidth(tmp);
            }
        }
        result.add(current);

        String[] arrayResult = new String[result.size()];
        for (int i = 0; i < arrayResult.length; i++) {
            arrayResult[i] = result.get(i);
        }

        return arrayResult;
    }

    /**
      * Split a string using the char code of a character.
      * @param s   the string to split
      * @param c   the integer code of the character
      * @return   an array containing the split string (or empty if the string is null)
      */
    public final static String[] split(String s, int c) {
        List<String> list = new ArrayList<String>();
        if (s != null) {
            int pos = 0, end;
            while ((end = s.indexOf(c, pos)) >= 0) {
                String sub = s.substring(pos, end);
                list.add(sub);
                pos = end + 1;
            }
            list.add(s.substring(pos));
        }
        if (list.isEmpty()) {
            return null;
        }
        int size = list.size();
        if (list.get(size - 1).isEmpty()) {
            size--;
        }

        String[] result = new String[size];
        for (int i = 0; i < size; i++) {
            result[i] = list.get(i);
        }

        return result;
    }
}

Related

  1. paintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal, float vertical)
  2. render(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign)
  3. setComponentSize(Component component, int rows, int columns)
  4. setSizedFont(Graphics g, String text, float maxFontSize, int maxWidth)
  5. shortenString(FontMetrics fm, String str, int maxWidth)
  6. splitText(String text, String delim, FontMetrics fontMetrics, int width)
  7. stringByTruncatingToFitInWidth(String s, int width, Graphics g, String truncatedSuffix)
  8. stringDimension(Graphics g, Font f, String s)
  9. StringDimension(Graphics g, String text)