-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameButton.java
More file actions
48 lines (42 loc) · 1.46 KB
/
Copy pathGameButton.java
File metadata and controls
48 lines (42 loc) · 1.46 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.UIManager;
class GameButton extends JButton {
private char symbol;
public GameButton() {
setPreferredSize(new Dimension(100, 100));
setFont(new Font("Arial", Font.BOLD, 40));
// Add mouse listener to handle hover effects
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
setBackground(Color.white); // Change background color on hover
}
@Override
public void mouseExited(MouseEvent e) {
setBackground(UIManager.getColor("Button.background")); // Restore original background color
}
});
}
public void setSymbol(char symbol) {
this.symbol = symbol;
repaint(); // Redraw the button to update its appearance
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (symbol == 'X') {
// Draw X symbol
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
} else if (symbol == 'O') {
// Draw O symbol
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
}
}