get String To Color Map - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

get String To Color Map

Demo Code


//package com.java2s;
import java.awt.Color;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<String, Color> getStringToColorMap() {
        Map<String, Color> ret = new HashMap<String, Color>();
        try {/*from   w  w w .  j a va  2  s . co m*/
            Class clazz = java.awt.Color.class;
            Field[] filc = clazz.getDeclaredFields();

            for (int i = 0; i < filc.length; i++) {
                // System.out.println(filc[i].getName());
                Field f = filc[i];

                if (Modifier.isStatic(f.getModifiers())
                        && Modifier.isPublic(f.getModifiers())) {
                    Object o = f.get(null);

                    if (o instanceof java.awt.Color) {
                        ret.put(f.getName(), (Color) o);
                    }
                }
            }
            return ret;
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ret;
    }
}

Related Tutorials