Reputation: 305
Ok, so I'm sure this is really simple, but here goes nothing:
So say I'm calling the method master here, and color is one of the parameters:
master("RED");
Here is the master method:
public static void master(String color) {
g.setColor(Color.color)
}
Disregard the g.SetColor, it's from a class that you probably don't have, the point is that when I call the string value of "RED" I intend on the color to be set to red. Otherwise I'd have to do it manually like this:
g.setColor(Color.RED);
I'm just confused why java isn't recognizing the color value when I call the color String.
Upvotes: 2
Views: 3764
Reputation: 459
A Color
object and a String value representing that object are two completely different things.
If you want to get a java.awt.Color
object from a String such as "Red", then you have to use some sort of method to convert it to the color it represents.
Try this:
Color realColor = (Color) Color.class.getField(color).get(null);
g.setColor(realColor);
This will first get the corresponding Color object for that String, then after it gets a Color
object it will call the setColor
method.
Edit: If all you require is just being able to specify the Color from another bit of code outside of that method (and it doesn't have to be a String), then you could always just pass it an actual Color object.
For example,
public static void master(Color color) {
g.setColor(color);
}
and you would use this somewhere else as
master(Color.RED);
This makes the master()
method pretty useless (unless you're going to include more logic in there), but it definitely beats having to parse a String color when you don't need to.
Upvotes: 1
Reputation: 13483
Color.RED
is calling a constant value. You can't call Color."RED"
RED
refers to a static field in the class Color
and you can't call that with a String. It's the same as trying to call a variable in your code with a String. It doesn't work that way.
Instead, create this method:
public static void master(Color color) {
g.setColor(color);
}
and call it like this:
master(Color.RED);
However, you could use Color.parseColor instead if you would like to use a String parameter.
Perhaps:
public static void master(String color) {
g.setColor(Color.parseColor(color));
}
and call it like this:
master("#FF0000");
Upvotes: 3
Reputation: 31689
You have a parameter named color
. Color.color
looks for a member named color
in the Color
class, and there isn't one (I think). It has nothing to do with your color
parameter. The declarations like
static Color RED = <whatever>;
in the Color
package define names that you can use in your program. But those names are not stored in your program as strings that are available at runtime (except by reflection, but that may be too complicated).
You'll need a method that looks up the color-name string in some table, and converts it to a Color
. Since it looks like getColor
doesn't work, you may need to build your own Map<String,Color>
and initialize it like
m.put("RED", Color.RED);
m.put("BLUE", Color.BLUE);
...
This tutorial explains more about Map
s.
Upvotes: 3