[英]Convert String to java.awt.Color
I'm trying to convert a String
object to a java.awt.Color
object.我正在尝试将
String
对象转换为java.awt.Color
对象。
I am scanning the input color from an user.我正在扫描来自用户的输入颜色。 The user enters a color and the color is saved in a
String
.用户输入颜色,颜色保存在
String
。 I want to put that color into an ArrayList
of colors from the String
.我想将该颜色放入
String
颜色的ArrayList
中。 How do I do that?我怎么做?
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter your color\n" +
"BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
String str = sc.next();
str = str.toUpperCase();
private ArrayList<Color> colorArray= new ArrayList<Color>();
// Here I want to put the colors (string str) in the colorArray arraylist.
How do I achieve this?我如何实现这一目标?
How about doing it through reflection?如何通过反思来做到这一点?
Color color = (Color)Color.class.getField(str).get(null);
colorArray.add(color);
You might want to do some exception handling in case the user enters a color that is not a field in the Color
class.如果用户输入的颜色不是
Color
类中的字段,您可能需要进行一些异常处理。
However, note that this technique will work only for certain basic colors for which the class java.awt.Color
provide static instance members.但是,请注意,此技术仅适用于
java.awt.Color
类为其提供静态实例成员的某些基本颜色。 Eg the class currently provides the following static instance members only:例如, 该类目前仅提供以下静态实例成员:
For other colors like turquoise, you will have to build the Color
object using an appropriate RGB combination.对于绿松石等其他颜色,您必须使用适当的 RGB 组合构建
Color
对象。
Here is an example of how you could do it with a bit of Java 9:下面是一个示例,说明如何使用一些 Java 9 来做到这一点:
public static void main(String[] args) throws ParseException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter your color\n" +
"BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
List<Color> colorArray= new ArrayList<>();
Map<String, Color> colorMap = Map.ofEntries(entry("BLUE", Color.BLUE),
entry( "BLACK", Color.BLACK),
entry( "ORANGE", Color.ORANGE)); // TODO: add more colours
while(sc.hasNext()) {
String next = sc.next();
Color c = colorMap.get(next);
if(c == null) {
if("END".equals(next)) {
break;
}
System.err.printf("Sorry, could not find %s%n", next);
}
else {
colorArray.add(c);
System.out.printf("Added %s%n", c);
}
}
System.out.println(colorArray);
}
This is the output of a sample run:这是示例运行的输出:
Enter your color
BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:
> BLUE
Added java.awt.Color[r=0,g=0,b=255]
> BLACK
Added java.awt.Color[r=0,g=0,b=0]
> ORANGE
Added java.awt.Color[r=255,g=200,b=0]
> END
[java.awt.Color[r=0,g=0,b=255], java.awt.Color[r=0,g=0,b=0], java.awt.Color[r=255,g=200,b=0]]
Here is another version based on @VHS ideas using reflection:这是使用反射基于@VHS 想法的另一个版本:
public static void main(String[] args) throws ParseException, ClassNotFoundException, IllegalAccessException {
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter your color\n" +
"BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
List<Color> colorArray= new ArrayList<>();
Class<Color> colorClass = Color.class;
while(sc.hasNext()) {
String next = sc.next();
try {
Color c = colorClass.cast(colorClass.getField(next.toLowerCase()).get(null));
colorArray.add(c);
System.out.printf("Added %s%n", c);
} catch (NoSuchFieldException e) {
if("END".equals(next)) {
break;
}
System.err.printf("Sorry, could not find %s%n", next);
}
}
System.out.println(colorArray);
}
Ideally you would combine both ideas (use a map and reflection), so that you support the declared colours + non declared colours in java.awt.Color.理想情况下,您将结合这两个想法(使用地图和反射),以便您支持 java.awt.Color 中声明的颜色 + 未声明的颜色。
public class TestProj {
public static void main (String[] args)throws IOException {
List<Color> colorArray= new ArrayList<Color>();
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter your color\n" +
"BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:");
while(sc.hasNext()){
String str = sc.next();
str = str.toUpperCase();
colorArray.add(new Color(str));
}
System.out.println("Array size: " + colorArray.size());
}
static class Color {
private String color;
public Color(String color){
setColor(color);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
System.out.println(color);
}
}
}
My output is (Ctrl-Z to terminate loop): Enter your color BLUE, BLACK, ORANGE, WHITE, YELLOW, RED, GREEN, PINK:我的输出是(Ctrl-Z 终止循环):输入您的颜色 BLUE、BLACK、ORANGE、WHITE、YELLOW、RED、GREEN、PINK:
BLUE BLACK ORANGE蓝黑橙
BLUE蓝色
BLACK黑色的
ORANGE橘子
Array size: 3数组大小:3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.