Java Rectangle Create Parse stringToRectangle(String string, Rectangle defaultValue)

Here you can find the source of stringToRectangle(String string, Rectangle defaultValue)

Description

Convert a string in the format of x,y,width,height in to a Rectangle object.

License

Open Source License

Parameter

Parameter Description
string string in format <code>x,y,width,height</code>
defaultValue default rectangle

Return

rectangle

Declaration

public static Rectangle stringToRectangle(String string, Rectangle defaultValue) 

Method Source Code

//package com.java2s;
/*/*from   w ww. j a  v a  2 s  .c  o m*/
*  Adito
*
*  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
*  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 2 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, write to the Free Software
*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

import java.awt.Rectangle;

import java.util.StringTokenizer;

public class Main {
    /**
     * Convert a string in the format of <code>x,y,width,height</code> in to a
     * {@link Rectangle} object. Suitable for retrieving rectangles from
     * property files, XML files etc. The supplied default value will be
     * returned if the string is not in the correct format or is
     * <code>null</code>.
     * 
     * @param string string in format <code>x,y,width,height</code>
     * @param defaultValue default rectangle
     * @return rectangle
     */
    public static Rectangle stringToRectangle(String string, Rectangle defaultValue) {
        if (string == null) {
            return defaultValue;
        }
        StringTokenizer t = new StringTokenizer(string, ","); //$NON-NLS-1$
        try {
            return new Rectangle(Integer.parseInt(t.nextToken()), Integer.parseInt(t.nextToken()),
                    Integer.parseInt(t.nextToken()), Integer.parseInt(t.nextToken()));
        } catch (Exception e) {
            return defaultValue;
        }
    }
}

Related

  1. storeBounds(String name, Window window)
  2. stringToBounds(String str, Rectangle defaultRect)
  3. stringToRectangle(String s)
  4. stringToRectangle(String s, Rectangle defaultValue)
  5. stringToRectangle(String str)
  6. stringToRectangle(String value)