How To Print String representation of Color in Java

◇◆丶佛笑我妖孽 提交于 2019-11-28 07:07:35

问题


I have an array of colours of size n. In my program, the number of teams is always <= n, and I need to assign each team a unique color. This is my color array:

private static Color[] TEAM_COLORS = {Color.BLUE, Color.RED, Color.CYAN, Color.GREEN, Color.ORANGE, Color.PINK};

When I print information about the players in the console, I want to print what color is associated with them. When I print the color, I get

java.awt.Color[r=...,g=...,b=...]. 

I understand that this is how Java prints colours. I was wondering if there was a way to instead print BLUE, RED, etc. (so the pre-defined color string).


回答1:


Extending @Jon_Skeet reply by adding name also to the enum.

public enum NamedColor {
  BLUE(Color.BLUE, "Blue"),
  RED(Color.RED, "Red"),
  ...;

  private final Color awtColor;
  private final String colorName;

  private NamedColor(Color awtColor, String name) {
    this.awtColor = awtColor;
    this.colorName = name;
  }

  public Color getAwtColor() {
    return awtColor;
  }

  public String getColorName() {
    return colorName;
  }
}

NOTE: IF voting this pls vote @Jon_Skeet reply too as it is extension of that...




回答2:


One option would be to create a NamedColor enum:

public enum NamedColor {
    BLUE(Color.BLUE),
    RED(Color.RED),
    ...;

    private final Color awtColor;

    private NamedColor(Color awtColor) {
        this.awtColor = awtColor;
    }

    public Color getAwtColor() {
        return awtColor;
    }
}

You'd then make your TEAM_COLORS array an array of NamedColor values instead of Color values, and fetch the AWT color when you need it. The default toString implementation of an enum is its name.

Another alternative would be to create your own Map<Color, String> and consult that when you need the string representation for a color.




回答3:


Here's a Reflection-based approach:

public static String getColorName(Color c) {
    for (Field f : Color.class.getFields()) {
        try {
            if (f.getType() == Color.class && f.get(null).equals(c)) {
                return f.getName();
            }
        } catch (java.lang.IllegalAccessException e) {
            // it should never get to here
        } 
    }
    return "unknown";
}

Examples:

getColorName(Color.BLACK); // black
getColorName(Color.BLUE); // blue
getColorName(new Color(0,1,2)); // unknown

Demo: http://ideone.com/6cIBD


This will only work with colors defined as fields in java.awt.Color, namely: white, light gray, gray, dark gray, black, red, pink, orange, yellow, green, magenta, cyan and blue.




回答4:


You might create a class that stores both a String representing the color name, as well as the Color itself.




回答5:


If you want your NamedColor to be used as a java.awt.Color and you don't have many colors you can extend it and store the name.

public class NamedColor extends java.awt.Color {

    private String name;

    public NamedColor(String name, java.awt.Color c) {
        super(c.getRGB());
        this.name = name;
    }

    public String toString() {
        return name;
    }
}



回答6:


You can try using String.valueOf(color.getRGB())



来源:https://stackoverflow.com/questions/9223903/how-to-print-string-representation-of-color-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!