-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreboard.java
85 lines (79 loc) · 2.38 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
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
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* A JPanel that displays information about player's turns and their key inventory.
* Uses the singleton design pattern.
*
* @author Mohamed Haryz Izzudin bin Mohamed Rafy (1141127874)
*/
public class Scoreboard extends JPanel {
/**
* The singleton instance for the scoreboard.
*/
private static Scoreboard instance = new Scoreboard();
/**
* The board model.
*/
private Board board;
/**
* Constructor for the Scoreboard class.
* Sets the layout for the JPanel, gets the board instance and paints the scoreboard.
*
* @author Haryz
*/
private Scoreboard() {
super(new GridLayout(4, 7));
board = Board.getInstance();
refreshScoreboard();
}
/**
* Gets the instance of the scoreboard.
*
* @author Haryz
* @return The instance of the scoreboard.
*/
public static Scoreboard getInstance() {
return instance;
}
/**
* Repaints the scoreboard with the current data from the board.
*
* @author Haryz
*/
public void refreshScoreboard() {
for (Component c : getComponents()) {
remove(c);
}
ArrayList<Player> players = board.getAllPlayers();
for (int i = 0; i < 4; i++) {
Player player = players.get(i);
JLabel playerLabel = new JLabel(player.getIcon());
if (board.getCurrentPlayer().equals(player)) {
playerLabel.setBackground(Color.ORANGE);
playerLabel.setOpaque(true);
}
add(playerLabel);
add(new JLabel(player.getPlayerName()));
ArrayList<Key> playerKeys = player.getKeys();
for (int j = 0; j < 5; j++) {
JLabel keyLabel;
if (j < playerKeys.size()) {
Key key = playerKeys.get(j);
keyLabel = new JLabel(key.getIcon());
}
else {
keyLabel = new JLabel("Empty");
}
if (j == 0) {
keyLabel.setBackground(Color.GREEN);
keyLabel.setOpaque(true);
}
add(keyLabel);
}
}
}
}