Java Rectangle Create Parse getRectangle(String rectStr)

Here you can find the source of getRectangle(String rectStr)

Description

Return a Rectangle from a string of "x,y,w,h".

License

Open Source License

Parameter

Parameter Description
rectStr Rectangle as a comma delimited String.

Return

A Rectangle decoded from the String.

Declaration

public static Rectangle getRectangle(String rectStr) 

Method Source Code

//package com.java2s;
/*// w  ww. j  a  v  a2 s . c  o m
 * JOGRE (Java Online Gaming Real-time Engine) - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://jogre.sourceforge.org
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Rectangle;

import java.util.StringTokenizer;

public class Main {
    /**
     * Return a Rectangle from a string of "x,y,w,h".  Where (x,y) is the
     * upper left hand corner, and w,h is the width & height.
     *
     * @param rectStr    Rectangle as a comma delimited String.
     * @return           A Rectangle decoded from the String.
     */
    public static Rectangle getRectangle(String rectStr) {
        try {
            StringTokenizer st = new StringTokenizer(rectStr, ",");
            int x = Integer.parseInt(st.nextToken().trim());
            int y = Integer.parseInt(st.nextToken().trim());
            int w = Integer.parseInt(st.nextToken().trim());
            int h = Integer.parseInt(st.nextToken().trim());
            return new Rectangle(x, y, w, h);
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. decodeBounds(String sBounds)
  2. getRectangle(Map bounds)
  3. getRectangle(Preferences prefs, String sName, int iDefaultWidth, int iDefaultHeight)
  4. getRectangle(Properties prop, final String key, final Rectangle def)
  5. newRectangle(int x1, int y1, int x2, int y2)
  6. rectangleToString(Rectangle r)
  7. rectangleToString(Rectangle r)
  8. rectangleToString(Rectangle rectangle)