@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Color startColor = Color.RED;
Color endColor = new Color(0, 0, 255);
int startX = 0;
int startY = 0;
int endX = getWidth();
int endY = getHeight();
// A non-cyclic gradient
GradientPaint gradient = new GradientPaint(startX, startY, startColor,
endX, endY, endColor);
// A cyclic gradient
gradient = new GradientPaint(startX + getWidth() / 2, startY
+ getHeight() / 2, startColor, endX, endY, endColor, true);
g2d.setPaint(gradient);
Rectangle2D rect = new Rectangle2D.Float(0, 0, getWidth(), getHeight());
g2d.fill(rect);
}