Java examples for 2D Graphics:Color
Converts a string into a Color object.
import java.awt.Color; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; import javax.swing.text.html.StyleSheet; public class Main{ static Map<String, Color> stringColorMap = GraphicsUtil .getStringToColorMap();//from w w w .ja va 2 s. co m static StyleSheet styleSheet = new StyleSheet(); /** * Converts a string into a Color object. * Checks a few local maps and the styleSheet * class. * @param s * @return */ public static Color getColorFromString(String s) { Color c = null; try { if (GraphicsUtil.stringColorMap.containsKey(s)) { return GraphicsUtil.stringColorMap.get(s); } else if (isDouble(s)) { float value = (float) Double.parseDouble(s); c = new Color(value, value, value); return c; } else { c = styleSheet.stringToColor(s); if (c != null) { return c; } } System.out.println("No color matching " + s); } catch (Exception e) { e.printStackTrace(); } return c; } private static boolean isDouble(String s) { try { Double.parseDouble(s); return true; } catch (NumberFormatException nfe) { return false; } } }