package jrummikub.view.impl; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import jrummikub.util.Event; import jrummikub.util.IEvent; /** * A panel that is displayed when a player has won */ @SuppressWarnings("serial") class WinPanel extends JPanel { private final static int PANEL_INSET = 15; private final static int PANEL_SEPARATOR = 10; private final static float PANEL_FIRST_LINE_HEIGHT = 0.375f; private final static int PANEL_MAX_WIDTH = 180; private final static float MAX_BUTTON_FONT_SIZE = 12; private JLabel winLabel; private JButton newRoundButton; private JButton finalScoreButton; private Event finalScoreEvent = new Event(); private Event newRoundEvent = new Event(); /** * Creates a new WinPanel */ WinPanel() { setLayout(null); setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET, PANEL_INSET)); winLabel = new JLabel(); winLabel.setHorizontalAlignment(JLabel.CENTER); winLabel.setHorizontalTextPosition(JLabel.CENTER); winLabel.setVerticalAlignment(JLabel.CENTER); winLabel.setVerticalTextPosition(JLabel.CENTER); add(winLabel); newRoundButton = new JButton("Neue Runde"); newRoundButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { newRoundEvent.emit(); } }); add(newRoundButton); finalScoreButton = new JButton("Endauswertung"); finalScoreButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { finalScoreEvent.emit(); } }); add(finalScoreButton); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { rescale(); } }); } /** * Sets the name of the current player * * @param name * the player name */ void setCurrentPlayerName(String name) { winLabel.setText("Du hast gewonnen, " + name + "!"); } /** * The new round event is emitted when the player wants to start a new round * * @return the event */ IEvent getNewRoundEvent() { return newRoundEvent; } /** * The quit event is emitted when the player wants to quit the program * * @return the event */ IEvent getFinalScoreEvent() { return finalScoreEvent; } private void rescale() { Insets insets = getInsets(); int x = insets.left, y = insets.top, width = getWidth() - insets.left - insets.right, height = getHeight() - insets.top - insets.bottom; if (width > PANEL_MAX_WIDTH) { x += (width - PANEL_MAX_WIDTH) / 4; width = width / 2 + PANEL_MAX_WIDTH / 2; } int firstLineHeight = (int) ((height - PANEL_SEPARATOR) * PANEL_FIRST_LINE_HEIGHT); int buttonWidth = (width - PANEL_SEPARATOR) / 2; int buttonHeight = height - PANEL_SEPARATOR - firstLineHeight; float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; if (fontSize > MAX_BUTTON_FONT_SIZE) fontSize = MAX_BUTTON_FONT_SIZE; winLabel.setBounds(x, y, width, firstLineHeight); newRoundButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR, buttonWidth, buttonHeight); newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize)); finalScoreButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y + firstLineHeight + PANEL_SEPARATOR, buttonWidth, buttonHeight); finalScoreButton.setFont(finalScoreButton.getFont().deriveFont(fontSize)); } }