If I have a string like:
String myColor = "Color.RED";
How do I get that to work in:
graphics.setColor(myColor);
I guess I’m asking how do I pass a variable object name to a function. I’ve tried a bunch of stuff and can not get it working.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’ll need error checking to make sure the string is valid, but the crux of it is this:
graphics.setColor((Color) Color.class.getField("RED").get(null));Of course, you’ll also have to use string manipulation to take the “Color.” part out of the string.