I am setting up a large-scale graphical interface (more than everything I did before) using the Java Swing toolkit, and I would like to set up my own color scheme for painting colors so that all the color definitions are in one place. To do this, I decided to create a top-level pseudostatic class with the name (used from https://stackoverflow.com/questions/12865/ ... ) which contains where the programmer sets the color scheme for the entire graphical interface. ColorPalette SchemeEnum
I would like the color choice to be independent of the knowledge of the color scheme. Does anyone know a design template or an effective way to do this? I'm not quite sure that my current setup is the best way to implement this, but I would like to create a modular design where it would not be annoying to add more or (at compile time, rather than at run time). ColorEnums SchemeEnums
For clarification, I want the programmer to simply select and return an object based on and specific . ColorEnumjava.awt.Color ColorEnum SchemeEnum
For instance:
// Use the BASIC color scheme
ColorPalette.setCurrentScheme(ColorPalette.SchemeEnum.BASIC);
// Set button backgrounds
testButton.setBackground(ColorPalette.ColorEnum.DARK_RED.getColor());
testButton2.setBackground(ColorPalette.ColorEnum.BLUE.getColor());
should return different objects than Color
// Use the DARK color scheme
ColorPalette.setCurrentScheme(ColorPalette.SchemeEnum.DARK);
// Set button backgrounds
testButton.setBackground(ColorPalette.ColorEnum.DARK_RED.getColor());
testButton2.setBackground(ColorPalette.ColorEnum.BLUE.getColor());
SchemeEnums, ColorPalette. , SchemeEnum ( ).
, HashTables , , , . ?
. !
package common.lookandfeel;
import java.awt.Color;
public final class ColorPalette
{
public static enum SchemeEnum
{
BASIC, DARK, METALLIC
}
public static enum ColorEnum
{
LIGHT_RED(256,0,0), RED(192,0,0), DARK_RED(128,0,0),
LIGHT_GREEN(0,256,0), GREEN(0,192,0), DARK_GREEN(0,128,0),
LIGHT_BLUE(0,0,256), BLUE(0,0,192), DARK_BLUE(0,0,128),
LIGHT_ORANGE(256,102,0), ORANGE(256,102,0), DARK_ORANGE(192,88,0),
LIGHT_YELLOW(256,204,0), YELLOW(256,204,0), DARK_YELLOW(192,150,0),
LIGHT_PURPLE(136,0,182), PURPLE(102,0,153), DARK_PURPLE(78,0,124);
private int red;
private int green;
private int blue;
private ColorEnum(int r, int g, int b)
{
this.red = r;
this.green = g;
this.blue = b;
}
public Color getColor()
{
return new Color(red, green, blue);
}
}
private static SchemeEnum currentScheme = SchemeEnum.BASIC;
private ColorPalette()
{
}
public static SchemeEnum getCurrentScheme()
{
return currentScheme;
}
public static void setCurrentScheme(SchemeEnum cp)
{
currentScheme = cp;
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Test Environment");
CustomButton testButton = new CustomButton ("Hello World");
CustomButton testButton2 = new CustomButton ("I am a button!");
ColorPalette.setCurrentScheme(ColorPalette.SchemeEnum.BASIC);
testButton.setBackground(ColorPalette.ColorEnum.DARK_RED.getColor());
testButton2.setBackground(ColorPalette.ColorEnum.BLUE.getColor());
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(testButton, BorderLayout.NORTH);
frame.getContentPane().add(testButton2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame = null;
testButton = null;
testButton2 = null;
System.gc();
}
}