-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreBoard.java
53 lines (41 loc) · 1.2 KB
/
ScoreBoard.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
50
51
52
53
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class ScoreBoard extends JPanel {
int po;
JLabel jl;
ScoreBoard(int flag) {
jl = new JLabel();
po = 2;
// Set initial text
jl.setText("2");
// Use a layout manager to center the label
setLayout(new GridBagLayout());
// Add label to panel
add(jl);
// Set a border for the panel
Border br = new LineBorder(Color.BLACK, 5, true);
setBorder(br);
// Set background color
setBackground(Color.cyan);
// Set text color based on the flag
if (flag == 1) {
jl.setForeground(Color.white);
} else {
jl.setForeground(Color.black);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Dynamically adjust font size based on the panel's height
int fontSize = getHeight() / 3;
jl.setFont(new Font("MV Boli", Font.BOLD, fontSize));
// Center the text in the label
jl.setHorizontalAlignment(SwingConstants.CENTER);
}
void updateBy(int x) {
po += x;
jl.setText("" + po);
}
}