How to get rid of magic numbers background

379 views Asked by At

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?

2

There are 2 answers

0
Hash On BEST ANSWER

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);
0
Jack On

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);