0

I have a program where I want to set the color of a JPanel when its object is created through its constructor. Right now this is what I have.

However this is obviously quite inefficient. I was wondering if there was some way I could pass the string parameter directly into the setBackground() method to set the color?

MyPanel(String bcolor) {
        super();
        if (bcolor.equals("red"))
            setBackground(Color.RED);
        if (bcolor.equals("blue"))
            setBackground(Color.BLUE);
        if (bcolor.equals("green"))
            setBackground(Color.GREEN);
        if (bcolor.equals("white"))
            setBackground(Color.WHITE);
        if (bcolor.equals("black"))
            setBackground(Color.BLACK);

    } 
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
  • *"I could pass the string parameter directly into the setBackground()"* - No. You could use a `Map` look up table instead – MadProgrammer Jul 09 '15 at 22:11
  • `"However this is obviously quite slow and inefficient."` -- why do you think that this is "obviously" so? What is slow or inefficient with if blocks? – Hovercraft Full Of Eels Jul 09 '15 at 22:14
  • @MadProgrammer . I actually dont know what Map is but I did a quick google search and that could work. I might try something like that thanks! – Ashwin Gupta Jul 09 '15 at 22:15
  • @HovercraftFullOfEels inefficient and slow in the sense that it requires me to type every possible color in an if block and this takes many lines of code. Not literally slow, sorry wrong word there. – Ashwin Gupta Jul 09 '15 at 22:16

2 Answers2

1

I was wondering if there was some way I could pass the string parameter directly into the setBackground() method to set the color?

No, obviously, as there is no setBackground(String) method.

Now, there are a number of possible solutions you might employee, your current series of if statements is one solution, another might be to use some king of static Map which acts as a look up between the String value and the Color you want to use, for example...

public class MyPanel extends JPanel {

    protected static final Map<String, Color> COLOR_MAP = new HashMap<>();

    public MyPanel(String color) {
        setBackground(COLOR_MAP.get(color.toLowerCase()));
    }

    static {
        COLOR_MAP.put("red", Color.RED);
        COLOR_MAP.put("blue", Color.BLUE);
        COLOR_MAP.put("green", Color.GREEN);
        COLOR_MAP.put("white", Color.WHITE);
        COLOR_MAP.put("black", Color.BLACK);
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I suppose you could use reflection to get the color associated with a particular String, but I'm not sure that it's worth the trouble. [For example](http://stackoverflow.com/a/5519656/522444). 1+ – Hovercraft Full Of Eels Jul 09 '15 at 22:19
  • @HovercraftFullOfEels That then becomes a runtime inefficiency (reflection been well known for it's runtime overhead). You could wrap this kind of thing into a utility class, so you only need to generate the look up table once and re-use it where ever you need to... – MadProgrammer Jul 09 '15 at 22:21
  • Okay thanks for the answer, this seems like a good way to do it also but unfortunately my knowledge of java is not that great and I havent yet learned about Maps. I will deffinetly try to learn about this but for now the RGB thing that dbillz said will probably work better for my program. Thanks! – Ashwin Gupta Jul 09 '15 at 22:22
  • 1
    @AshwinGupta What ever works ;). Personally, I'd either pass it a `Color` object or a packed `int` value, which `Color` can decode – MadProgrammer Jul 09 '15 at 22:25
  • Im using the Color object and RGB int values as the parameters. Thanks again @MadProgrammer! – Ashwin Gupta Jul 09 '15 at 22:29
0

The simplest way would be to pass it the RGB values for the color you want in the constructor.

MyPanel(int r, int g, int b){
    super();
    setBackground(new Color(r,g,b));
}

If you really, really, want to use Strings, you could do this

String colorName; //note that this has to be exact spelling and capitalization of the fields in the Color class.
Field field = Class.forName("java.awt.Color").getField(colorName);
setBackground((Color) field.get(null));
dbillz
  • 210
  • 1
  • 7
  • Ah okay, I thought about that, I guess that would work but It would be better if I could just specify the color name that I wanted. RGB would require me to look things up alot because I havent memorized how to make any colors using the values (except for red, blue, and green obviously) – Ashwin Gupta Jul 09 '15 at 22:12
  • Check out the edit, also for reference there are some pretty awesome websites for finding the color values of things like this one http://www.rapidtables.com/web/color/RGB_Color.htm – dbillz Jul 09 '15 at 22:15
  • I think the use of reflection as far more inefficient then the use of a series of `if` statements :P – MadProgrammer Jul 09 '15 at 22:16
  • Thanks for the link @dbillz . That could make this a little easier. I think I might just have to go with the RGB thing. – Ashwin Gupta Jul 09 '15 at 22:19