import javax.swing.*;
import java.awt.*;
public class GradientButton extends JButton {
public GradientButton(String name) {
this.setText(name);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font("Arial", Font.BOLD, 14));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(new LinearGradientPaint(0.0f, 0.0f, 0.0f, (float) getHeight(), new float[]{0.0f, 0.30f, 0.60f,
0.9f}, new Color[]{new Color(0x63a5f7), new Color(0x3799f4), new Color(0x2d7eeb),
new Color(0x30a5f9)}));
g2d.fillRect(0, 0, getWidth(), getHeight());
int width = getFontMetrics(getFont()).stringWidth(getText());
int height = getFont().getSize();
g2d.setColor(Color.YELLOW);
g2d.drawString(getText(), (getWidth() / 2 - width / 2), (getHeight() / 2 + height / 2));
}
}