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 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| /**
* Classe permettant de dessiner un bouton avec un fond dégradé
*/
package org.leasysudoku.design;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GradientButton extends JButton {
/** serial Version UID */
private static final long serialVersionUID = 1L;
/** Couleur de début */
private Color startColor;
/** Couleur de fin */
private Color endColor;
/** Couleur de début par défaut */
public static final Color START_COLOR = new Color(242, 242, 242);
/** Couleur de fin par défaut */
public static final Color END_COLOR = new Color(220, 220, 220);
/**
* Constructeur par défaut de la classe GradientButton
*/
public GradientButton() {
this(GradientButton.START_COLOR, GradientButton.END_COLOR);
}
/**
* Constructeur de la classe GradientButton permettant de construire un bouton avec
* un fond dégradé
* @param startColor Couleur de début
* @param endColor Couleur de fin
*/
public GradientButton(Color startColor, Color endColor) {
this.startColor = startColor;
this.endColor = endColor;
}
/**
* Paints each of the components in this container
* @param g The graphics context
*/
protected void paintComponent(Graphics g) {
final Graphics2D g2 = (Graphics2D) g;
int w = getWidth();
int h = getHeight();
GradientPaint gradient = new GradientPaint(0, 0, startColor, 0, h, endColor, false);
g2.setPaint(gradient);
g2.fillRect(0, 0, w, h);
//ImageIcon i = new ImageIcon("images/toolbar/new.png");
//g2.drawImage(i.getImage(), 0, 0, this);
}
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setSize(600,400);
GradientButton button = new GradientButton();
button.setForeground(Color.BLACK);
button.setText("Test");
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
/**
* @return Returns the endColor.
*/
public Color getEndColor() {
return endColor;
}
/**
* @param endColor The endColor to set.
*/
public void setEndColor(Color endColor) {
this.endColor = endColor;
repaint();
}
/**
* @return Returns the startColor.
*/
public Color getStartColor() {
return startColor;
}
/**
* @param startColor The startColor to set.
*/
public void setStartColor(Color startColor) {
this.startColor = startColor;
repaint();
}
} |
Partager