get Rectangle from Properties - Java java.util

Java examples for java.util:Properties File

Description

get Rectangle from Properties

Demo Code


//package com.java2s;

import java.awt.Rectangle;

import java.util.Properties;

public class Main {
    /**/*from   ww  w. j a  va 2s .  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 Tutorials