Reputation: 1645
In this method I make a string variable, and move it to a different one.
private void produceRainbow() {
String color = "RED";
produceArc(color);
}
There is more code there but it does not matter (essentially, changing the string to other colors).
Next this method:
private void produceArc(String color) {
GOval arc = new GOval(leftX, upperY, rightX, lowerY);
arc.setColor(Color.color);
}
(Ignore the variables leftX, upperY, rightX, lowerY)
Here I want to set the color to a string.
So I want it to become arc.setColor(Color.RED)
When I compile, I get this error:
Program.java:89: cannot find symbol
symbol : variable color
location: class java.awt.Color
arc.setColor(Color.color);
Is it even possible to do what I want to do? If so, what am I doing wrong?
(If you're curious, I made a seperate method for each arc (red, blue, green, etc, all have their own method) and this works, but I was wondering if I could just use one method that takes a variable, which makes the code a lot shorter)
Upvotes: 0
Views: 30399
Reputation: 1036
To elaborate the comment I gave.
You could either pass the Color.RED right away or use Color.FromName(string name);
Suggested method:
To pass the Color.RED your methods will look like this:
private void produceRainbow() {
Color color = Color.RED;
produceArc(color);
}
And:
private void produceArc(Color color) {
GOval arc = new GOval(leftX, upperY, rightX, lowerY);
arc.setColor(color);
}
Method 2
If, for some reason, you would use the FromName method you'd apply it like below:
private void produceRainbow() {
String color = "Red";
produceArc(color);
}
And:
private void produceArc(String color) {
GOval arc = new GOval(leftX, upperY, rightX, lowerY);
arc.setColor(Color.FromName(color));
}
Note that this last method is only working in C# and is not suitable for Java. (I'm not sure whether you're using Java or C#)
Upvotes: 1