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 before a player's turn */ @SuppressWarnings("serial") class StartTurnPanel 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 startTurnLabel; private JButton startTurnButton; private Event startTurnEvent = new Event(); /** * Creates a new StartTurnPanel */ StartTurnPanel() { setLayout(null); setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET, PANEL_INSET)); startTurnLabel = new JLabel(); startTurnLabel.setHorizontalAlignment(JLabel.CENTER); startTurnLabel.setHorizontalTextPosition(JLabel.CENTER); startTurnLabel.setVerticalAlignment(JLabel.CENTER); startTurnLabel.setVerticalTextPosition(JLabel.CENTER); add(startTurnLabel); startTurnButton = new JButton("Zug beginnen"); startTurnButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { startTurnEvent.emit(); } }); add(startTurnButton); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { rescale(); } }); } /** * Sets the current player name * * @param playerName * the player name */ void setCurrentPlayerName(String playerName) { startTurnLabel.setText("'" + playerName + "' ist jetzt an der Reihe."); } /** * The start turn event is emitted when the current player has clicked the * start turn button * * @return the event */ IEvent getStartTurnEvent() { return startTurnEvent; } 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; 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; startTurnLabel.setBounds(x, y, width, firstLineHeight); startTurnButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR, buttonWidth, buttonHeight); startTurnButton.setFont(startTurnButton.getFont().deriveFont(fontSize)); } }