-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomButton.java
49 lines (44 loc) · 1.64 KB
/
CustomButton.java
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
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class CustomButton extends JButton {
// Constructor
public CustomButton(String text, int width, int height, int x, int y) {
super(text);
setForeground(Color.WHITE);
setPreferredSize(new Dimension(width, height));
setBorder(new ShadowBorder());
setFocusPainted(false);
setRolloverEnabled(true);
setContentAreaFilled(false);
setBounds(x, y, width, height);
// Set custom UI for the button
setUI(new BasicButtonUI() {
@Override
public void update(Graphics g, JComponent c) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
if (model.isPressed()) {
g.setColor(Color.BLACK);
} else if (model.isRollover()) {
g.setColor(Color.DARK_GRAY.darker());
} else {
g.setColor(Color.DARK_GRAY);
}
g.fillRoundRect(0, 0, b.getWidth(), b.getHeight(), 15, 15);
}
super.update(g, c);
}
});
try {
// Load custom font
Font ehsFont = Font.createFont(Font.TRUETYPE_FONT, new File("fonts\\ElectronicHighwaySign.TTF")).deriveFont(18f);
setFont(ehsFont);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}
}