Java Rectangle Create Parse getRectangle(Properties prop, final String key, final Rectangle def)

Here you can find the source of getRectangle(Properties prop, final String key, final Rectangle def)

Description

get Rectangle

License

Open Source License

Parameter

Parameter Description
prop The Properties set
key the key used to store this property
def a default in case the property cannot be retrieved

Declaration

public static Rectangle getRectangle(Properties prop, final String key, final Rectangle def) 

Method Source Code


//package com.java2s;

import java.awt.Rectangle;

import java.util.Properties;

public class Main {
    /**//from   w w w .  ja  v  a  2 s . c o  m
     * @param prop The Properties set
     * @param key the key used to store this property
     * @param def a default in case the property cannot be retrieved
     */
    public static Rectangle getRectangle(Properties prop, final String key, final Rectangle def) {
        try {
            final Rectangle result = new Rectangle();
            result.x = getInteger(prop, key.concat("-x"));
            result.y = getInteger(prop, key.concat("-y"));
            result.width = getInteger(prop, key.concat("-w"));
            result.height = getInteger(prop, key.concat("-h"));
            return result;
        } catch (Exception e) {
            return def;
        }
    }

    /**
     * @exception NumberFormatException if the property retrieved cannot be converted to <code>int</code>
     * @param prop The Properties set
     * @param key the key used to store this property
     */
    public static int getInteger(Properties prop, final String key) throws NumberFormatException {
        return Integer.parseInt(prop.getProperty(key));
    }

    /**
     * @param prop The Properties set
     * @param key the key used to store this property
     * @param def a default in case the property cannot be retrieved
     */
    public static int getInteger(Properties prop, final String key, final int def) {
        try {
            final String s = prop.getProperty(key);
            return s == null ? def : Integer.parseInt(s);
        } catch (Exception e) {
            return def;
        }
    }
}

Related

  1. decodeBounds(String sBounds)
  2. getRectangle(Map bounds)
  3. getRectangle(Preferences prefs, String sName, int iDefaultWidth, int iDefaultHeight)
  4. getRectangle(String rectStr)
  5. newRectangle(int x1, int y1, int x2, int y2)
  6. rectangleToString(Rectangle r)
  7. rectangleToString(Rectangle r)