get Color from Properties - Java java.util

Java examples for java.util:Properties File

Description

get Color from Properties

Demo Code


//package com.java2s;
import java.awt.Color;

import java.util.Properties;

public class Main {
    /**/*from w ww  .  j a v  a 2  s . co 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 Color getColor(Properties prop, final String key,
            final java.awt.Color def) {
        try {
            return new Color(getInteger(prop, key.concat("-r")),
                    getInteger(prop, key.concat("-g")), getInteger(prop,
                            key.concat("-b")));
        } 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