Here you can find the source of stringToRectangle(String string, Rectangle defaultValue)
x,y,width,height
in to a Rectangle object.
Parameter | Description |
---|---|
string | string in format <code>x,y,width,height</code> |
defaultValue | default rectangle |
public static Rectangle stringToRectangle(String string, Rectangle defaultValue)
//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; } } }