You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.8 KiB

package de.tims.leaderboard;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.*;
import de.tims.player_management.Player;
public class Leaderboard {
private static final int NUM_PLAYERS_ON_LEADERBOARD = 10;
private List<Player> allPlayers;
String actualPlayer;
public Leaderboard(List<Player> allPlayers, String actualPlayer) {
this.allPlayers = allPlayers;
this.actualPlayer = actualPlayer;
}
public Player[] getTop10Players() {
Player[] top10Players = new Player[NUM_PLAYERS_ON_LEADERBOARD];
for (Player actualPlayer : allPlayers) {
for (int i = 0; i < NUM_PLAYERS_ON_LEADERBOARD; i++) {
if (top10Players[i] == null) {
top10Players[i] = actualPlayer;
break;
} else if (actualPlayer.getPoints() > top10Players[i].getPoints()) {
Player playerToInsert = actualPlayer;
Player tmp;
for (int j = i; j < NUM_PLAYERS_ON_LEADERBOARD; j++) {
tmp = top10Players[j];
top10Players[j] = playerToInsert;
playerToInsert = tmp;
}
break;
}
}
}
return top10Players;
}
public JPanel buildLeaderboard() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel place;
JLabel name;
JLabel points;
Player[] top10Players = getTop10Players();
for (int i = -1; i < top10Players.length; i++) {
if (i < 0) {
place = new JLabel("Platzierung");
name = new JLabel("Name");
points = new JLabel("Punkte");
} else {
place = new JLabel((i + 1) + ".");
name = new JLabel(top10Players[i].getName());
points = new JLabel("" + top10Players[i].getPoints());
if (top10Players[i].getName().equals(actualPlayer)) {
Font actualPlayerFont = new Font(Font.DIALOG, Font.PLAIN, 20);
place.setForeground(Color.RED);
place.setFont(actualPlayerFont);
name.setForeground(Color.RED);
name.setFont(actualPlayerFont);
points.setForeground(Color.RED);
points.setFont(actualPlayerFont);
}
}
gbc.gridx = 0;
gbc.gridy = i + 1;
gbc.weightx = 0.5;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(5, 0, 5, 5);
mainPanel.add(place, gbc);
gbc.gridx = 1;
gbc.gridy = i + 1;
gbc.weightx = 0.0;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(5, 5, 5, 10);
mainPanel.add(name, gbc);
gbc.gridx = 2;
gbc.gridy = i + 1;
gbc.weightx = 0.5;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(5, 10, 5, 0);
mainPanel.add(points, gbc);
}
return mainPanel;
}
}