private void paintGradient(Gradient gradient, Graphics g) {
Insets insets = getInsets();
Graphics2D g2d = (Graphics2D) g;
GradientPaint gp = null;
Direction direction = gradient.getDirection();
Color firstColor = gradient.getFirstColor();
Color secondColor = gradient.getSecondColor();
int y = insets.top;
int x = insets.left;
int w = getWidth() - (insets.left + insets.right);
int h = getHeight() - (insets.bottom + insets.top);
if (direction.equals(Direction.TOP_TO_BOTTOM)) {
gp = new GradientPaint(0, y, firstColor, 0, h, secondColor);
} else if (direction.equals(Direction.BOTTOM_TO_TOP)) {
gp = new GradientPaint(0, y, secondColor, 0, h, firstColor);
} else if (direction.equals(Direction.LEFT_TO_RIGHT)) {
gp = new GradientPaint(x, 0, firstColor, w, 0, secondColor);
} else if (direction.equals(Direction.RIGHT_TO_LEFT)) {
gp = new GradientPaint(x, 0, secondColor, w, 0, firstColor);
}
g2d.setPaint(gp);
g2d.fillRect(x, y, w, h);
}