Retrieving a Predefined Color by Name - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Retrieving a Predefined Color by Name

Demo Code

import java.awt.Color;
import java.lang.reflect.Field;

public class Main {
  public Color getColor(String colorName) {
    try {/*from www .j  a v a2s  .  c o  m*/
      // Find the field and value of colorName
      Field field = Class.forName("java.awt.Color").getField(colorName);
      return (Color) field.get(null);
    } catch (Exception e) {
      return null;
    }
  }
}

Related Tutorials