From 2cc2b6333dfcb4cb1d45f622dde83269bc45d3de Mon Sep 17 00:00:00 2001 From: Tobias Krause Date: Sat, 22 Jan 2022 13:36:11 +0100 Subject: [PATCH] leaderboard: added method buildLeaderboard --- .../java/de/tims/leaderboard/Leaderboard.java | 13 ++++++++ .../tims/leaderboard/LeaderboardGUITest.java | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/de/tims/leaderboard/LeaderboardGUITest.java diff --git a/src/main/java/de/tims/leaderboard/Leaderboard.java b/src/main/java/de/tims/leaderboard/Leaderboard.java index c7fd4c8..3e0d377 100644 --- a/src/main/java/de/tims/leaderboard/Leaderboard.java +++ b/src/main/java/de/tims/leaderboard/Leaderboard.java @@ -1,6 +1,7 @@ package de.tims.leaderboard; import java.util.List; +import javax.swing.*; import de.tims.player_management.Player; @@ -40,5 +41,17 @@ public class Leaderboard { return top10Players; } + + public JPanel buildLeaderboard() { + JPanel mainPanel = new JPanel(); + + Player[] top10Players = getTop10Players(); + + for (int i = 0; i < top10Players.length; i++) { + mainPanel.add(new JLabel(i + 1 + ". " + top10Players[i].getName() + " " + top10Players[i].getPoints())); + } + + return mainPanel; + } } diff --git a/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java b/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java new file mode 100644 index 0000000..2e25051 --- /dev/null +++ b/src/main/java/de/tims/leaderboard/LeaderboardGUITest.java @@ -0,0 +1,32 @@ +package de.tims.leaderboard; + +import java.util.List; + +import javax.swing.*; + +import de.tims.player_management.Player; + +public class LeaderboardGUITest { + + private JFrame frame; + private JPanel panel; + Leaderboard leaderboard; + + public LeaderboardGUITest() { + frame = new JFrame("LeaderboardGUITest"); + leaderboard = new Leaderboard(List.of(new Player("Eins", 0), new Player("Zwei", 130), new Player("Drei", 20), new Player("Vier", 10), + new Player("Fuenf", 20), new Player("Sechs", 70), new Player("Sieben", 60), new Player("Acht", 20), + new Player("Neun", 60), new Player("Zehn", 50), new Player("Elf", 90), new Player("Zwoelf", 20), + new Player("Dreizehn", 40), new Player("Vierzehn", 80)), "Neun"); + panel = leaderboard.buildLeaderboard(); + + frame.add(panel); + frame.setSize(240, 240); + frame.setVisible(true); + } + + public static void main(String[] args) { + new LeaderboardGUITest(); + } + +}