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 103 104 105 106
| public class MonBouton extends JButton {
private static final long serialVersionUID = 1L;
private String texte = "";
private Font font = new Font("Dialog", 0, 15);
public MonBouton(){
this("");
}
public MonBouton(String text){
this.texte = text;
if(texte.contains("<br>")){
setText("");
setLayout(new GridLayout(0, 1));
String[] st = texte.split("<br>");
for (int i=0;i<st.length;i++) {
String s = st[i];
//temporaire, j'enlèverai ca après
s = s.replaceAll("<html>", "");
s = s.replaceAll("</html>", "");
s = s.replaceAll("<center>", "");
JLabel lbl = new JLabel(s, JLabel.CENTER);
System.out.println("s" + s); //ca c'est ok
lbl.setBorder(BorderFactory.createLineBorder(Color.pink));
add(lbl);
}
}else{
setText(text);
}
setFont(font);
setFocusable(false);
setContentAreaFilled(false);
setPreferredSize(new Dimension(100, 65));
}
public void paintComponent(Graphics g) {
//dans le cas ou le bouton a une icone
if(getIcon() != null){
String icnPath = getIcon().toString();
String debut = icnPath.substring(0, icnPath.length() - 4);
String ext = icnPath.substring(icnPath.length() - 4, icnPath.length());
String newPath = debut + Global.IMG_PRESSED + ext;
ImageIcon icn = new ImageIcon(newPath);
if(getModel().isPressed()){
if(icn.getIconWidth() > 0){
setIcon(icn);
}
}else{
icn = new ImageIcon(newPath.replace(Global.IMG_PRESSED, ""));
if(icn.getIconWidth() > 0){
setIcon(icn);
}
}
setBorder(null);
super.paintComponent(g);
}else{
Graphics2D g2 = (Graphics2D) g;
//couleur lors du clic
if(getModel().isPressed()){
if(texte.contains("Abandon")){
g.setColor(Color.red.darker());
}else if(texte.contains("Total")){
g.setColor(new Color(0,148,0));
}else{
g.setColor(new Color(153, 153, 153));
}
}
//couleur en fonctionnement normal
else{
Color color1;
Color color2;
if(texte.contains("Abandon")){
g.setColor(Color.red);
}else if(texte.contains("Total")){
g.setColor(new Color(144,238,144));
}else{
color1 = new Color(242, 242, 242);
color2 = new Color(150, 150, 150);
g2.setPaint((Paint) new GradientPaint(0, 0, color1, 0, this.getHeight(), color2));
}
}
//tracement du cadre
g2.fillRect(3, 3, getWidth() - 6, getHeight() - 6);
super.paintComponent(g);
//couleur du cadre
g2.setColor(Color.black);
//arrondi des angles du cadre
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//taille du cadre
g2.setStroke(new BasicStroke(1.5f));
g2.draw(new RoundRectangle2D.Double(1, 1, (getWidth() - 3), (getHeight() - 3), 12, 8));
g2.drawLine(4, getHeight() - 3, getWidth() - 4, getHeight() - 3);
g2.dispose();
}
}
} |
Partager