summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/WinPanel.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/view/impl/WinPanel.java')
-rw-r--r--src/jrummikub/view/impl/WinPanel.java104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/jrummikub/view/impl/WinPanel.java b/src/jrummikub/view/impl/WinPanel.java
new file mode 100644
index 0000000..cd63540
--- /dev/null
+++ b/src/jrummikub/view/impl/WinPanel.java
@@ -0,0 +1,104 @@
+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;
+
+public 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 newGameButton;
+ private JButton quitButton;
+
+ private Event quitEvent = new Event();
+ private Event newGameEvent = new Event();
+
+ WinPanel() {
+ setLayout(null);
+ setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET,
+ PANEL_INSET));
+
+ winLabel = new JLabel("Du hast gewonnen!");
+ winLabel.setHorizontalAlignment(JLabel.CENTER);
+ winLabel.setHorizontalTextPosition(JLabel.CENTER);
+ winLabel.setVerticalAlignment(JLabel.CENTER);
+ winLabel.setVerticalTextPosition(JLabel.CENTER);
+ add(winLabel);
+
+ newGameButton = new JButton("Neues Spiel");
+ newGameButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent arg0) {
+ newGameEvent.emit();
+ }
+ });
+ add(newGameButton);
+
+ quitButton = new JButton("Beenden");
+ quitButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent arg0) {
+ quitEvent.emit();
+ }
+ });
+ add(quitButton);
+
+ addComponentListener(new ComponentAdapter() {
+ @Override
+ public void componentResized(ComponentEvent e) {
+ rescale();
+ }
+ });
+ }
+
+ IEvent getNewGameEvent() {
+ return newGameEvent;
+ }
+
+ IEvent getQuitEvent() {
+ return quitEvent;
+ }
+
+ 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);
+ newGameButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR,
+ buttonWidth, buttonHeight);
+ newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
+
+ quitButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y
+ + firstLineHeight + PANEL_SEPARATOR, buttonWidth, buttonHeight);
+ quitButton.setFont(quitButton.getFont().deriveFont(fontSize));
+ }
+}