1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package jgradient;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
/**
*
* @author
*/
public class JGradientButton extends JButton {
private Color topColor;
private Color bottomColor;
private Color borderColor;
/** Creates a new instance of JGradientButton */
public JGradientButton() {
this(new Color(255, 255, 255), new Color(234, 235, 237));
}
public JGradientButton(Color c1, Color c2) {
this.topColor = c1;
this.bottomColor = c2;
this.borderColor = new Color(167, 166, 170);
this.setBorder(BorderFactory.createLineBorder(borderColor));
this.setContentAreaFilled(false);
this.setFocusPainted(false);
}
public void setTopColor(Color c1) {
this.topColor = c1;
repaint();
}
public void setBottomColor(Color c2) {
this.bottomColor = c2;
repaint();
}
public void setBorderColor(Color color) {
this.borderColor = color;
repaint();
}
protected void paintComponent(Graphics g) {
final Graphics2D g2 = (Graphics2D) g;
int w = getWidth();
int h = getHeight();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gradient = new GradientPaint(0, 0, topColor, 0, h, bottomColor, false);
g2.setPaint(gradient);
g2.fillRect(0, 0, w, h);
super.paintComponent(g);
}
/*
* Main
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JGradientButton button = new JGradientButton();
//button.setForeground(Color.BLACK);
button.setText("Test 1212");
button.setIcon(new ImageIcon("icon.gif"));
button.setOpaque(false);
frame.getContentPane().add(button);
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
} |
Partager