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
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Test extends JFrame {
private JButtonG1 btn;
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn = new JButtonG1("test");
btn.setForeground(Color.WHITE);
btn.setBackground(new Color(255, 0, 0, 128));
JPanel content = (JPanel) getContentPane();
content.setBackground(Color.YELLOW);
content.setBorder(BorderFactory.createEmptyBorder(5, 2, 2, 2));
content.add(btn);
}
public static void main(String[] args) {
Test fenetre = new Test();
fenetre.pack();
fenetre.setLocationRelativeTo(null);
fenetre.setVisible(true);// On la rend visible
}
public class JButtonG1 extends JButton {
public JButtonG1(String titre) {
super.setText(titre);
setBorderPainted(false);
setContentAreaFilled(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(getBackground());
int h = getHeight();
int arc = h / 3;
g2.fillRoundRect(0, 0, getWidth(), h, arc, arc);
super.paintComponent(g2);
}
}
} |
Partager