klasscho

LeaderboardsPanel

Jun 8th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package com.gui;
  2.  
  3. import com.game.DrawUtils;
  4. import com.game.Game;
  5. import com.game.Leaderboards;
  6. import java.awt.Color;
  7. import java.awt.Font;
  8. import java.awt.Graphics2D;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.util.ArrayList;
  12. import java.util.Iterator;
  13.  
  14. public class LeaderboardsPanel extends GuiPanel {
  15.     private Leaderboards lBoard;
  16.     private int buttonWidth = 100;
  17.     private int backButtonWidth = 220;
  18.     private int buttonSpacing = 20;
  19.     private int buttonY = 120;
  20.     private int buttonHeight = 50;
  21.     private int leaderboardsX = 130;
  22.     private int leaderboardsY;
  23.     private String title;
  24.     private Font titleFont;
  25.     private Font scoreFont;
  26.     private LeaderboardsPanel.State currentState;
  27.  
  28.     public LeaderboardsPanel() {
  29.         this.leaderboardsY = this.buttonY + this.buttonHeight + 90;
  30.         this.title = "Leaderboards";
  31.         this.titleFont = Game.main.deriveFont(48.0F);
  32.         this.scoreFont = Game.main.deriveFont(30.0F);
  33.         this.currentState = LeaderboardsPanel.State.SCORE;
  34.         this.lBoard = Leaderboards.getInstance();
  35.         this.lBoard.loadScores();
  36.         GuiButton tileButton = new GuiButton(Game.WIDTH / 2 - this.buttonWidth / 2, this.buttonY, this.buttonWidth, this.buttonHeight);
  37.         tileButton.addActionListener(new ActionListener() {
  38.             public void actionPerformed(ActionEvent e) {
  39.                 LeaderboardsPanel.this.currentState = LeaderboardsPanel.State.TILE;
  40.             }
  41.         });
  42.         tileButton.setText("Tiles");
  43.         this.add(tileButton);
  44.         GuiButton scoreButton = new GuiButton(Game.WIDTH / 2 - this.buttonWidth / 2 - tileButton.getWidth() - this.buttonSpacing, this.buttonY, this.buttonWidth, this.buttonHeight);
  45.         scoreButton.addActionListener(new ActionListener() {
  46.             public void actionPerformed(ActionEvent e) {
  47.                 LeaderboardsPanel.this.currentState = LeaderboardsPanel.State.SCORE;
  48.             }
  49.         });
  50.         scoreButton.setText("Scores");
  51.         this.add(scoreButton);
  52.         GuiButton timeButton = new GuiButton(Game.WIDTH / 2 + this.buttonWidth / 2 + this.buttonSpacing, this.buttonY, this.buttonWidth, this.buttonHeight);
  53.         timeButton.addActionListener(new ActionListener() {
  54.             public void actionPerformed(ActionEvent e) {
  55.                 LeaderboardsPanel.this.currentState = LeaderboardsPanel.State.TIME;
  56.             }
  57.         });
  58.         timeButton.setText("Times");
  59.         this.add(timeButton);
  60.         GuiButton backButton = new GuiButton(Game.WIDTH / 2 - this.backButtonWidth / 2, 500, this.backButtonWidth, 60);
  61.         backButton.addActionListener(new ActionListener() {
  62.             public void actionPerformed(ActionEvent e) {
  63.                 GuiScreen.getInstance().setCurrentPanel("Menu");
  64.             }
  65.         });
  66.         backButton.setText("Back");
  67.         this.add(backButton);
  68.     }
  69.  
  70.     private void drawLeaderboards(Graphics2D g) {
  71.         ArrayList<String> strings = new ArrayList();
  72.         if (this.currentState == LeaderboardsPanel.State.SCORE) {
  73.             strings = this.convertToStrings(this.lBoard.getTopScores());
  74.         } else if (this.currentState == LeaderboardsPanel.State.TILE) {
  75.             strings = this.convertToStrings(this.lBoard.getTopTiles());
  76.         }
  77.  
  78.         g.setColor(Color.black);
  79.         g.setFont(this.scoreFont);
  80.  
  81.         for(int i = 0; i < strings.size(); ++i) {
  82.             String s = i + 1 + ". " + (String)strings.get(i);
  83.             g.drawString(s, this.leaderboardsX, this.leaderboardsY + i * 40);
  84.         }
  85.  
  86.     }
  87.  
  88.     private ArrayList<String> convertToStrings(ArrayList<? extends Number> list) {
  89.         ArrayList<String> ret = new ArrayList();
  90.         Iterator var4 = list.iterator();
  91.  
  92.         while(var4.hasNext()) {
  93.             Number n = (Number)var4.next();
  94.             ret.add(n.toString());
  95.         }
  96.  
  97.         return ret;
  98.     }
  99.  
  100.     public void update() {
  101.     }
  102.  
  103.     public void render(Graphics2D g) {
  104.         super.render(g);
  105.         g.setColor(Color.black);
  106.         g.drawString(this.title, Game.WIDTH / 2 - DrawUtils.getMessageWidth(this.title, this.titleFont, g) / 2, DrawUtils.getMessageHeight(this.title, this.titleFont, g) + 40);
  107.         this.drawLeaderboards(g);
  108.     }
  109.  
  110.     private static enum State {
  111.         SCORE,
  112.         TILE,
  113.         TIME;
  114.  
  115.         private State() {
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment