Java Rectangle Create Parse stringToBounds(String str, Rectangle defaultRect)

Here you can find the source of stringToBounds(String str, Rectangle defaultRect)

Description

Parses the string into a Rectangle.

License

Open Source License

Parameter

Parameter Description
str the string in the form "x,y,w,h"
defaultRect the default rectangle to use if the string can't be parsed

Declaration

public static Rectangle stringToBounds(String str, Rectangle defaultRect) 

Method Source Code

//package com.java2s;

import java.awt.Rectangle;

public class Main {
    public static Rectangle stringToBounds(String str) {
        return stringToBounds(str, null);
    }/*from  w ww .  j  av a2  s. com*/

    /**
     * Parses the string into a Rectangle.
     * @param str the string in the form "x,y,w,h"
     * @param defaultRect the default rectangle to use if the string can't be parsed
     */
    public static Rectangle stringToBounds(String str, Rectangle defaultRect) {
        Rectangle rect = defaultRect;
        if ((str != null) && (str.length() > 0)) {
            String[] xywh = str.split(",");
            if (xywh.length == 4) {
                try {
                    Rectangle r = new Rectangle(Integer.parseInt(xywh[0]), // X
                            Integer.parseInt(xywh[1]), // Y
                            Integer.parseInt(xywh[2]), // WIDTH
                            Integer.parseInt(xywh[3])); // HEIGHT
                    rect = r;
                } catch (Exception ignore) {
                }
            }
        }
        return rect;
    }

    public static int parseInt(String s) {
        return parseInt(s, 0);
    }

    public static int parseInt(String s, int def) {
        int rv = def;
        if ((s != null) && (s.length() > 0)) {
            try {
                rv = Integer.parseInt(s);
            } catch (NumberFormatException ignore) {
            }
        }
        return rv;
    }
}

Related

  1. newRectangle(int x1, int y1, int x2, int y2)
  2. rectangleToString(Rectangle r)
  3. rectangleToString(Rectangle r)
  4. rectangleToString(Rectangle rectangle)
  5. storeBounds(String name, Window window)
  6. stringToRectangle(String s)
  7. stringToRectangle(String s, Rectangle defaultValue)
  8. stringToRectangle(String str)
  9. stringToRectangle(String string, Rectangle defaultValue)