-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameButton.java
More file actions
81 lines (68 loc) · 2.36 KB
/
Copy pathGameButton.java
File metadata and controls
81 lines (68 loc) · 2.36 KB
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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class is used to create the main screen buttons
*/
import java.awt.*;
import javax.swing.*;
// Custom JButton class with a unique appearance
public class GameButton extends JButton {
// Background color of the button
public Color baseColor;
public boolean hovered;
public int arc = 16;
// Constructor to initialize the button with text and default styles
GameButton(String txt) {
super(txt); // Set the button text
baseColor = new Color(80, 60, 120); // Default background color
setFocusPainted(false); // Disable focus painting
setBorderPainted(false); // Disable border painting
setContentAreaFilled(false); // Disable default content area fill
setForeground(Color.WHITE); // Set text color to white
addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
hovered = true;
repaint();
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
hovered = false;
repaint();
}
});
}
// Method to update the background color
public void setColor(Color color) {
baseColor = color;
repaint();
}
// Override the paintComponent method to customize the button's appearance
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = null;
int w = 0;
int h = 0;
Color c1 = null;
Color c2 = null;
GradientPaint gp = null;
g2 = (Graphics2D) g.create(); // Create a copy of the Graphics object
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
w = getWidth();
h = getHeight();
c1 = hovered ? baseColor.brighter() : baseColor;
c2 = c1.darker();
gp = new GradientPaint(0, 0, c1, 0, h, c2);
g2.setPaint(gp);
g2.fillRoundRect(0, 0, w, h, arc, arc);
super.paintComponent(g2); // Draw text
g2.setStroke(new BasicStroke(3f));
g2.setColor(Color.WHITE);
g2.drawRoundRect(0, 0, w - 1, h - 1, arc, arc);
g2.dispose();
}
@Override
public boolean isOpaque() {
return false;
}
}