In .NET you can achieve something like this:
Color yellowColor = Color.FromName("yellow");
Is there a way of doing this in Java without having to resort to reflection?
PS: I am not asking for alternative ways of storing/loading colors. I just want to know wherever it is possible to do this or not.
In . NET you can achieve something like this: Color yellowColor = Color. FromName("yellow");
String color="RED" int c1=parseInt(color) new Color(c1);
Use reflection to access the static member of the Color
class.
Color color;
try {
Field field = Class.forName("java.awt.Color").getField("yellow");
color = (Color)field.get(null);
} catch (Exception e) {
color = null; // Not defined
}
I tried something like this and it worked (at least for JavaFX)
String color = "red";
Color c = Color.web(color);
gc.setFill(color);
gc.fillOval(10, 10, 50, 40);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With