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
|
public class Bouton extends JButton implements MouseListener{
private String name ;
private GradientPaint gp ;
public Bouton(String name){
super(name) ;
gp = new GradientPaint(0,0, Color.BLUE,0, this.getHeight(), Color.CYAN, false) ;
this.name = name ;
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g ;
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.white);
g2d.drawString(name,7 * this.getWidth()/16, 3*this.getHeight()/4);
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
gp = new GradientPaint(0,0, Color.RED,0, this.getHeight(), Color.YELLOW, false) ;
}
public void mouseExited(MouseEvent arg0) {
gp = new GradientPaint(0,0, Color.BLUE,0, this.getHeight(), Color.CYAN, false) ;
}
public void mousePressed(MouseEvent arg0) {
gp = new GradientPaint(0,0, Color.GREEN,0, this.getHeight(), Color.ORANGE, false) ;
}
public void mouseReleased(MouseEvent mouse) {
if (mouse.getX() > 0 && mouse.getX() < getWidth() && mouse.getY() > 0 && mouse.getY() < getHeight() )
gp = new GradientPaint(0,0, Color.RED,0, this.getHeight(), Color.YELLOW, false) ;
else gp = new GradientPaint(0,0, Color.BLUE,0, this.getHeight(), Color.CYAN, false) ;
}
} |
Partager