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
| public class MonButton extends JButton implements MouseListener {
private static final long serialVersionUID = 1L;
private Color background=new Color(184, 207, 229);
private Color bordercolor=new Color(149, 194, 251);
private Color borderanime=new Color(149, 194, 251);
private Color foreground=Color.black;
private Font myfont=new Font("Palatino Linotype", Font.BOLD, 12);
private int marge=10;
boolean mouseexited=true;
boolean pressed=false;
public MonButton(){
super();
this.addMouseListener(this);
}
public MonButton(String text){
super(text);
this.addMouseListener(this);
this.addItemListener(this);
}
protected void paintComponent(Graphics g){
if(!pressed){
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(new GradientPaint(0, this.getHeight()-1, this.background, 0, this.getHeight()/3,Color.white,true));
g2.fillRoundRect(0, 0, this.getWidth()-1, this.getHeight()-1,22,12);
}
else{
g.setColor(background);
g.fillRoundRect(0, 0, this.getWidth()-1, this.getHeight()-1,22,12);
}
g.setFont(myfont);
g.setColor(foreground);
this.drawtext(g,this.getWidth(), this.getHeight());
}
private Dimension gettextsize(Graphics g,String s){
FontMetrics fm=g.getFontMetrics();
int w=fm.stringWidth(s);
int h=fm.getAscent()+fm.getDescent();
return new Dimension(w,h);
}
private void drawtext(Graphics g,int w,int h){
String ch=new String(this.getText());
Dimension d=this.gettextsize(g,ch);
FontMetrics fm=g.getFontMetrics();
if(d.width>w-2*marge){
int ind=this.getText().length()-1;
boolean b=true;
for(;b&&ind>=0;ind--){
b=fm.stringWidth(ch+"...")>w-2*marge;
ch=ch.substring(0, ind);
}
ch+="...";
d=this.gettextsize(g, ch);
}
int x=(w-d.width)/2;
int y=fm.getAscent()+(h-d.height)/2;
g.drawString(ch, x, y);
}
protected void paintBorder(Graphics g){
if(mouseexited)
g.setColor(bordercolor);
else{
g.setColor(this.borderanime);
g.drawRoundRect(1,1, this.getWidth()-2, this.getHeight()-2, 22, 12);
}
g.drawRoundRect(0, 0, this.getWidth()-1, this.getHeight()-1, 22, 12);
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
mouseexited=false;
this.repaint();
}
public void mouseExited(MouseEvent arg0) {
mouseexited=true;
this.repaint();
}
public void mousePressed(MouseEvent arg0) {
pressed=true;
this.repaint();
}
public void mouseReleased(MouseEvent arg0) {
pressed=false;
this.repaint();
}
} |
Partager