-4

I'm getting magic numbers for jpanel background. Is there a way to get rid of this since it's bad practice:

panel.setBackground(new color (255,255,0));

How can I have custom colors too?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Use constants. What do the numbers 55, 265, and 44 actually mean? Define those in some static final variable. – Makoto Nov 27 '13 at 03:33
  • 1
    that's not even valid Java - there is no `color` class, and `265` is not a valid color value – tckmn Nov 27 '13 at 03:37

2 Answers2

1

Just define your own palette somewhere, eg:

class Colors {
  public static final Color BACKGROUND_COLOR = new Color(55,265,44);
  public static final Color PALE_RED = new Color(...);
}

panel.setBackground(Colors.BACKGROUND_COLOR);
Jack
  • 131,802
  • 30
  • 241
  • 343
1

There are some common colors. such as red,blue, etc.

panel.setBackground(Color.YELLOW);

but if you want to set your own colors you must create a object with your colors,

public static final Color myColor = new Color(55,265,44);

then set it,

panel.setBackground(myColor);
Hash
  • 7,726
  • 9
  • 34
  • 53