Skip to content
Snippets Groups Projects
Commit 62c3695a authored by Johannes Link's avatar Johannes Link
Browse files

Removed duplication and cleaned up in ScorekeeperConsole

parent c45a7fde
Branches
Tags after-2-3
No related merge requests found
......@@ -2,25 +2,25 @@ package scorekeeper.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import scorekeeper.*;
public class ScorekeeperConsole extends JFrame implements ScoreViewer {
private static Font SCORE_FONT = new Font("Courier New", Font.BOLD, 48);
private static Font TIME_FONT = new Font("Courier New", Font.PLAIN, 24);
private static final Font SCORE_FONT = new Font("Courier New", Font.BOLD, 48);
private static final Font TIME_FONT = new Font("Courier New", Font.PLAIN, 24);
private static final String LABEL_RESET_BUTTON = "Reset";
private static final String LABEL_SCORE1_BUTTON = "1";
private static final String LABEL_SCORE2_BUTTON = "2";
private static final String LABEL_SCORE3_BUTTON = "3";
private static final String LABEL_TEAM_A_BUTTON = "Team A";
private static final String LABEL_TEAM_B_BUTTON = "Team B";
private static final String INITIAL_FORMATTED_TIME = "10:00";
private JLabel scoreALabel;
private JLabel scoreBLabel;
private JLabel timeLabel;
private JButton teamAButton;
private JButton teamBButton;
private JButton score1Button;
private JButton score2Button;
private JButton score3Button;
private JButton resetTimeButton;
private JButton pauseTimeButton;
private JButton continueTimeButton;
private final Scorekeeper scorekeeper;
private final GameTimeClock clock;
......@@ -57,12 +57,9 @@ public class ScorekeeperConsole extends JFrame implements ScoreViewer {
private Component clockButtonsPanel() {
JPanel clockButtonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
resetTimeButton = new JButton("Reset");
resetTimeButton.addActionListener(e -> clock.reset());
continueTimeButton = new JButton("\u25B6");
continueTimeButton.addActionListener(e -> clock.start());
pauseTimeButton = new JButton("\u25FC");
pauseTimeButton.addActionListener(e -> clock.stop());
JButton resetTimeButton = createButton(LABEL_RESET_BUTTON, e1 -> clock.reset());
JButton continueTimeButton = createButton("\u25B6", e -> clock.start());
JButton pauseTimeButton = createButton("\u25FC", e -> clock.stop());
clockButtonsPanel.add(resetTimeButton);
clockButtonsPanel.add(continueTimeButton);
clockButtonsPanel.add(pauseTimeButton);
......@@ -78,29 +75,31 @@ public class ScorekeeperConsole extends JFrame implements ScoreViewer {
private Component createPointsButtonsPanel() {
JPanel scoringButtonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
score1Button = new JButton("1");
score1Button.addActionListener(e -> scorekeeper.score1Clicked());
score2Button = new JButton("2");
score2Button.addActionListener(e -> scorekeeper.score2Clicked());
score3Button = new JButton("3");
score3Button.addActionListener(e -> scorekeeper.score3Clicked());
score1Button.setName("score1Point");
score2Button.setName("score2Point");
score3Button.setName("score3Point");
JButton score1Button = createNamedButton(LABEL_SCORE1_BUTTON, "score1Point", e -> scorekeeper.score1Clicked());
JButton score2Button = createNamedButton(LABEL_SCORE2_BUTTON, "score2Point", e -> scorekeeper.score2Clicked());
JButton score3Button = createNamedButton(LABEL_SCORE3_BUTTON, "score3Point", e -> scorekeeper.score3Clicked());
scoringButtonsPanel.add(score1Button);
scoringButtonsPanel.add(score2Button);
scoringButtonsPanel.add(score3Button);
return scoringButtonsPanel;
}
private JButton createNamedButton(String text, String name, ActionListener action) {
JButton score1Button = createButton(text, action);
score1Button.setName(name);
return score1Button;
}
private static JButton createButton(String text, ActionListener action) {
JButton newButton = new JButton(text);
newButton.addActionListener(action);
return newButton;
}
private Component createTeamButtonsPanel() {
JPanel teamButtonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
teamAButton = new JButton("Team A");
teamAButton.addActionListener(e -> scorekeeper.teamAClicked());
teamBButton = new JButton("Team B");
teamBButton.addActionListener(e -> scorekeeper.teamBClicked());
teamAButton.setName("teamA");
teamBButton.setName("teamB");
JButton teamAButton = createNamedButton(LABEL_TEAM_A_BUTTON, "teamA", e -> scorekeeper.teamAClicked());
JButton teamBButton = createNamedButton(LABEL_TEAM_B_BUTTON, "teamB", e -> scorekeeper.teamBClicked());
teamButtonsPanel.add(teamAButton);
teamButtonsPanel.add(teamBButton);
return teamButtonsPanel;
......@@ -115,29 +114,37 @@ public class ScorekeeperConsole extends JFrame implements ScoreViewer {
private JPanel createScorePanel() {
JPanel scorePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
scoreALabel = new JLabel();
scoreALabel.setFont(SCORE_FONT);
scoreBLabel = new JLabel();
scoreBLabel.setFont(SCORE_FONT);
scoreALabel = createScoringLabel("scoreA");
scoreBLabel = createScoringLabel("scoreB");
JLabel colon = new JLabel(":");
colon.setFont(SCORE_FONT);
scoreALabel.setName("scoreA");
scoreBLabel.setName("scoreB");
scorePanel.add(scoreALabel);
scorePanel.add(colon);
scorePanel.add(scoreBLabel);
return scorePanel;
}
private static JLabel createScoringLabel(String name) {
JLabel newLabel = new JLabel();
newLabel.setFont(SCORE_FONT);
newLabel.setName(name);
return newLabel;
}
private JPanel createTimePanel() {
timeLabel = new JLabel("10:00");
timeLabel.setFont(TIME_FONT);
timeLabel.setName("time");
timeLabel = createTimeLabel();
JPanel timePanel = new JPanel(new FlowLayout());
timePanel.add(timeLabel);
return timePanel;
}
private static JLabel createTimeLabel() {
JLabel label = new JLabel(INITIAL_FORMATTED_TIME);
label.setFont(TIME_FONT);
label.setName("time");
return label;
}
@Override
public void dispose() {
if (clock != null)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment