-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernButton.java
More file actions
190 lines (164 loc) · 6.92 KB
/
ModernButton.java
File metadata and controls
190 lines (164 loc) · 6.92 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
/**
* A modern, beautiful button with hover effects and smooth animations.
*/
public class ModernButton extends JButton {
private Color baseColor;
private Color hoverColor;
private Color pressedColor;
private Color currentColor;
private float animationProgress = 0f;
private Timer animationTimer;
private boolean isHovered = false;
private boolean isPressed = false;
private int cornerRadius = 12;
private int horizontalPadding = 34;
private int buttonHeight = 48;
public ModernButton(String text) {
this(text, BattleshipGUI.ACCENT_COLOR);
}
public ModernButton(String text, Color color) {
super(text);
this.baseColor = color;
this.hoverColor = brighter(color, 0.15f);
this.pressedColor = darker(color, 0.15f);
this.currentColor = baseColor;
setFont(BattleshipGUI.BUTTON_FONT);
setForeground(BattleshipGUI.TEXT_PRIMARY);
setPreferredSize(new Dimension(180, buttonHeight));
setMaximumSize(new Dimension(360, 54));
setMinimumSize(new Dimension(140, 42));
// Remove default button styling
setContentAreaFilled(false);
setBorderPainted(false);
setFocusPainted(false);
setOpaque(false);
setCursor(new Cursor(Cursor.HAND_CURSOR));
// Setup animation timer
animationTimer = new Timer(16, e -> {
if (isHovered && animationProgress < 1f) {
animationProgress = Math.min(1f, animationProgress + 0.15f);
repaint();
} else if (!isHovered && animationProgress > 0f) {
animationProgress = Math.max(0f, animationProgress - 0.15f);
repaint();
} else {
animationTimer.stop();
}
});
// Mouse listeners for effects
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
isHovered = true;
if (!animationTimer.isRunning()) {
animationTimer.start();
}
}
@Override
public void mouseExited(MouseEvent e) {
isHovered = false;
isPressed = false;
if (!animationTimer.isRunning()) {
animationTimer.start();
}
}
@Override
public void mousePressed(MouseEvent e) {
isPressed = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
isPressed = false;
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
// Calculate current color based on animation
currentColor = interpolateColor(baseColor, hoverColor, animationProgress);
if (isPressed) {
currentColor = pressedColor;
}
if (!isEnabled()) {
currentColor = blendColors(baseColor, BattleshipGUI.PANEL_BG, 0.30f);
}
// Draw shadow
if (isEnabled()) {
g2d.setColor(new Color(0, 0, 0, 40));
g2d.fill(new RoundRectangle2D.Float(2, 4, width - 4, height - 4, cornerRadius, cornerRadius));
}
// Draw button background with gradient
GradientPaint gradient = new GradientPaint(
0, 0, brighter(currentColor, 0.1f),
0, height, darker(currentColor, 0.1f)
);
g2d.setPaint(gradient);
g2d.fill(new RoundRectangle2D.Float(0, 0, width - 2, height - 4, cornerRadius, cornerRadius));
// Draw subtle highlight at top
g2d.setColor(new Color(255, 255, 255, 30));
g2d.fill(new RoundRectangle2D.Float(1, 1, width - 4, height / 3, cornerRadius - 2, cornerRadius - 2));
// Draw border
g2d.setColor(brighter(currentColor, 0.2f));
g2d.setStroke(new BasicStroke(1.5f));
g2d.draw(new RoundRectangle2D.Float(0.5f, 0.5f, width - 3, height - 5, cornerRadius, cornerRadius));
String text = getText();
if (text != null && !text.isEmpty()) {
g2d.setFont(getFont());
FontMetrics fm = g2d.getFontMetrics();
int textX = (width - fm.stringWidth(text)) / 2;
int textY = ((height - 4 - fm.getHeight()) / 2) + fm.getAscent();
g2d.setColor(isEnabled() ? BattleshipGUI.TEXT_PRIMARY : new Color(255, 255, 255, 215));
g2d.drawString(text, textX, textY);
}
g2d.dispose();
}
private Color interpolateColor(Color c1, Color c2, float fraction) {
int r = (int) (c1.getRed() + fraction * (c2.getRed() - c1.getRed()));
int g = (int) (c1.getGreen() + fraction * (c2.getGreen() - c1.getGreen()));
int b = (int) (c1.getBlue() + fraction * (c2.getBlue() - c1.getBlue()));
return new Color(r, g, b);
}
private Color brighter(Color c, float factor) {
int r = Math.min(255, (int) (c.getRed() + 255 * factor));
int g = Math.min(255, (int) (c.getGreen() + 255 * factor));
int b = Math.min(255, (int) (c.getBlue() + 255 * factor));
return new Color(r, g, b);
}
private Color darker(Color c, float factor) {
int r = Math.max(0, (int) (c.getRed() * (1 - factor)));
int g = Math.max(0, (int) (c.getGreen() * (1 - factor)));
int b = Math.max(0, (int) (c.getBlue() * (1 - factor)));
return new Color(r, g, b);
}
private Color blendColors(Color first, Color second, float secondWeight) {
float firstWeight = 1f - secondWeight;
int r = (int) (first.getRed() * firstWeight + second.getRed() * secondWeight);
int g = (int) (first.getGreen() * firstWeight + second.getGreen() * secondWeight);
int b = (int) (first.getBlue() * firstWeight + second.getBlue() * secondWeight);
return new Color(r, g, b);
}
@Override
public Dimension getPreferredSize() {
FontMetrics fm = getFontMetrics(getFont());
int textWidth = fm.stringWidth(getText() == null ? "" : getText());
int width = Math.max(160, textWidth + horizontalPadding * 2);
return new Dimension(width, buttonHeight);
}
public void setButtonColor(Color color) {
this.baseColor = color;
this.hoverColor = brighter(color, 0.15f);
this.pressedColor = darker(color, 0.15f);
repaint();
}
}