From 45d5b3ae10ed8cfbecb5489636093c6fb0576970 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 8 Jun 2011 21:58:16 +0200 Subject: Implement pause function git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@390 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/impl/PausePanel.java | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/jrummikub/view/impl/PausePanel.java (limited to 'src/jrummikub/view/impl/PausePanel.java') diff --git a/src/jrummikub/view/impl/PausePanel.java b/src/jrummikub/view/impl/PausePanel.java new file mode 100644 index 0000000..60e7038 --- /dev/null +++ b/src/jrummikub/view/impl/PausePanel.java @@ -0,0 +1,95 @@ +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 PausePanel 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 pauseLabel; + private JButton endPauseButton; + + private Event endPauseEvent = new Event(); + + /** + * Creates a new StartTurnPanel + */ + PausePanel() { + setLayout(null); + setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET, + PANEL_INSET)); + + pauseLabel = new JLabel(); + pauseLabel.setHorizontalAlignment(JLabel.CENTER); + pauseLabel.setHorizontalTextPosition(JLabel.CENTER); + pauseLabel.setVerticalAlignment(JLabel.CENTER); + pauseLabel.setVerticalTextPosition(JLabel.CENTER); + add(pauseLabel); + + endPauseButton = new JButton("Spiel fortsetzen"); + endPauseButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + endPauseEvent.emit(); + } + }); + add(endPauseButton); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + rescale(); + } + }); + } + + void setCurrentPlayerName(String playerName) { + pauseLabel.setText("Der Zug von " + playerName + " ist pausiert."); + } + + IEvent getEndPauseEvent() { + return endPauseEvent; + } + + 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; + + pauseLabel.setBounds(x, y, width, firstLineHeight); + endPauseButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR, + buttonWidth, buttonHeight); + endPauseButton.setFont(endPauseButton.getFont().deriveFont(fontSize)); + } +} -- cgit v1.2.3