From e39a539ee38a1413aac9f09a556a9ca4a181bc39 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Wed, 4 May 2011 15:40:34 +0200 Subject: Added StartTurnPanel git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@102 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/JRummikub.java | 12 +- src/jrummikub/view/IPlayerPanel.java | 70 ++--- src/jrummikub/view/IView.java | 66 +++-- src/jrummikub/view/impl/PlayerPanel.java | 432 ++++++++++++++-------------- src/jrummikub/view/impl/StartTurnPanel.java | 89 ++++++ src/jrummikub/view/impl/View.java | 158 +++++----- 6 files changed, 482 insertions(+), 345 deletions(-) create mode 100644 src/jrummikub/view/impl/StartTurnPanel.java (limited to 'src') diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index 0765dd5..9d32a2f 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -30,9 +30,9 @@ public class JRummikub { } catch (Exception e) { } - IView view = new jrummikub.view.impl.View(); + final IView view = new jrummikub.view.impl.View(); - view.getPlayerPanel().setCurrentPlayerName("Player 1"); + view.setCurrentPlayerName("Player 1"); view.getPlayerPanel().setTimeLeft(42); view.getTablePanel().setLeftPlayerName("Player 2"); view.getTablePanel().setTopPlayerName("Player 3"); @@ -54,6 +54,14 @@ public class JRummikub { @Override public void handle() { System.out.println("'End turn' fired"); + view.enableStartTurnPanel(true); + } + }); + view.getStartTurnEvent().add(new IListener() { + @Override + public void handle() { + System.out.println("'Start turn' fired"); + view.enableStartTurnPanel(false); } }); diff --git a/src/jrummikub/view/IPlayerPanel.java b/src/jrummikub/view/IPlayerPanel.java index dd24e33..86c1440 100644 --- a/src/jrummikub/view/IPlayerPanel.java +++ b/src/jrummikub/view/IPlayerPanel.java @@ -6,47 +6,39 @@ import jrummikub.util.IEvent; * The player panel that contains a player's board and other user interfaces */ public interface IPlayerPanel { - /** - * @return the board where the players hand stones are displayed - */ - public IHandPanel getHandPanel(); + /** + * @return the board where the players hand stones are displayed + */ + public IHandPanel getHandPanel(); - /** - * Sets the current player's name - * - * @param playerName - * the player name - */ - public void setCurrentPlayerName(String playerName); + /** + * Sets the time the player has left for his turn + * + * @param time + * the time left + */ + public void setTimeLeft(int time); - /** - * Sets the time the player has left for his turn - * - * @param time - * the time left - */ - public void setTimeLeft(int time); + /** + * The sort by groups event is emitted when the player wants to sort his + * stones by groups + * + * @return the event + */ + public IEvent getSortByGroupsEvent(); - /** - * The sort by groups event is emitted when the player wants to sort his - * stones by groups - * - * @return the event - */ - public IEvent getSortByGroupsEvent(); + /** + * The sort by runs event is emitted when the player wants to sort his stones + * by runs + * + * @return the event + */ + public IEvent getSortByRunsEvent(); - /** - * The sort by runs event is emitted when the player wants to sort his stones - * by runs - * - * @return the event - */ - public IEvent getSortByRunsEvent(); - - /** - * The end turn event is emitted when the player wants to end his turn - * - * @return the event - */ - public IEvent getEndTurnEvent(); + /** + * The end turn event is emitted when the player wants to end his turn + * + * @return the event + */ + public IEvent getEndTurnEvent(); } diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index 8e59baa..b43984b 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -3,30 +3,54 @@ package jrummikub.view; import java.util.Collection; import jrummikub.model.Stone; +import jrummikub.util.IEvent; /** * The top-level view interface */ public interface IView { - /** - * Returns the table - * - * @return the table - */ - public ITablePanel getTablePanel(); - - /** - * Returns the player panel - * - * @return the playerPanel - */ - public IPlayerPanel getPlayerPanel(); - - /** - * Sets the stones that are to be painted selected - * - * @param stones - * the stones to be painted selected - */ - public void setSelectedStones(Collection stones); + /** + * Returns the table + * + * @return the table + */ + public ITablePanel getTablePanel(); + + /** + * Returns the player panel + * + * @return the playerPanel + */ + public IPlayerPanel getPlayerPanel(); + + /** + * Sets the current player's name + * + * @param playerName + * the player name + */ + public void setCurrentPlayerName(String playerName); + + /** + * Sets the stones that are to be painted selected + * + * @param stones + * the stones to be painted selected + */ + public void setSelectedStones(Collection stones); + + /** + * Enables or disables the player's StartTurnPanel + * + * @param enable + * enable/disable + */ + public void enableStartTurnPanel(boolean enable); + + /** + * The start turn event is emitted when the player wants to start his turn + * + * @return the event + */ + public IEvent getStartTurnEvent(); } diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index 10bd111..111ec1a 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -23,221 +23,219 @@ import jrummikub.view.IPlayerPanel; */ @SuppressWarnings("serial") class PlayerPanel extends JPanel implements IPlayerPanel { - private final static int SIDE_PANEL_INSET = 15; - private final static int SIDE_PANEL_SEPARATOR = 10; - private final static float SIDE_PANEL_FIRST_LINE_HEIGHT = 0.375f; - private final static int SIDE_PANEL_MAX_WIDTH = 180; - private final static float MAX_BUTTON_FONT_SIZE = 12; - - private final static DecimalFormat secondFormat = new DecimalFormat("00"); - - private HandPanel hand; - - private JPanel leftPanel, rightPanel; - - private JLabel currentPlayerNameLabel; - private JButton sortByGroupsButton; - private JButton sortByRunsButton; - private JProgressBar timeBar; - private JButton endTurnButton; - - private Event sortByGroupsEvent = new Event(); - private Event sortByRunsEvent = new Event(); - private Event endTurnEvent = new Event(); - - @Override - public HandPanel getHandPanel() { - return hand; - } - - @Override - public void setCurrentPlayerName(String playerName) { - currentPlayerNameLabel.setText(playerName); - } - - @Override - public void setTimeLeft(int time) { - timeBar.setValue(time); - timeBar.setString(Integer.toString(time / 60) + ":" - + secondFormat.format(time % 60)); - } - - @Override - public IEvent getSortByGroupsEvent() { - return sortByGroupsEvent; - } - - @Override - public IEvent getSortByRunsEvent() { - return sortByRunsEvent; - } - - @Override - public IEvent getEndTurnEvent() { - return endTurnEvent; - } - - private void createLeftPanel() { - leftPanel = new JPanel(); - leftPanel.setLayout(null); - leftPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, - SIDE_PANEL_INSET, SIDE_PANEL_INSET)); - - currentPlayerNameLabel = new JLabel(); - currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER); - currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER); - currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER); - currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER); - leftPanel.add(currentPlayerNameLabel); - - sortByGroupsButton = new JButton("
Nach Gruppen sortieren"); - sortByGroupsButton.setFont(sortByGroupsButton.getFont().deriveFont(0)); - sortByGroupsButton.setMargin(new Insets(0, 0, 0, 0)); - sortByGroupsButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { - sortByGroupsEvent.emit(); - } - }); - leftPanel.add(sortByGroupsButton); - - sortByRunsButton = new JButton("
Nach Reihen sortieren"); - sortByRunsButton.setFont(sortByRunsButton.getFont().deriveFont(0)); - sortByRunsButton.setMargin(new Insets(0, 0, 0, 0)); - sortByRunsButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { - sortByRunsEvent.emit(); - } - }); - leftPanel.add(sortByRunsButton); - - leftPanel.addComponentListener(new LeftPanelResizeListener()); - } - - private void createRightPanel() { - rightPanel = new JPanel(); - rightPanel.setLayout(null); - rightPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, - SIDE_PANEL_INSET, SIDE_PANEL_INSET)); - - timeBar = new JProgressBar(0, 60); - timeBar.setStringPainted(true); - rightPanel.add(timeBar); - - endTurnButton = new JButton("Zug beenden"); - endTurnButton.setFont(endTurnButton.getFont().deriveFont(0)); - endTurnButton.setMargin(new Insets(0, 0, 0, 0)); - endTurnButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { - endTurnEvent.emit(); - } - }); - - rightPanel.add(endTurnButton); - - rightPanel.addComponentListener(new RightPanelResizeListener()); - } - - 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; - int boardWidth = hand.getWidth(); - int panelWidth = (width - boardWidth) / 2; - - leftPanel.setBounds(x, y, panelWidth, height); - hand.setBounds(x + panelWidth, y, boardWidth, height); - rightPanel.setBounds(x + panelWidth + boardWidth, y, panelWidth, height); - - leftPanel.validate(); - rightPanel.validate(); - } - - /** - * Creates a new PlayerPanel instance - */ - PlayerPanel() { - setLayout(null); - - createLeftPanel(); - add(leftPanel); - - hand = new HandPanel(); - add(hand); - - createRightPanel(); - add(rightPanel); - - ComponentListener rescaleListener = new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent e) { - rescale(); - } - }; - - addComponentListener(rescaleListener); - hand.addComponentListener(rescaleListener); - } - - private class LeftPanelResizeListener extends ComponentAdapter { - @Override - public void componentResized(ComponentEvent e) { - Insets insets = leftPanel.getInsets(); - int x = insets.left, y = insets.top, width = leftPanel.getWidth() - - insets.left - insets.right, height = leftPanel.getHeight() - - insets.top - insets.bottom; - - if (width > SIDE_PANEL_MAX_WIDTH) { - x += (width - SIDE_PANEL_MAX_WIDTH) / 4; - width = width / 2 + SIDE_PANEL_MAX_WIDTH / 2; - } - - int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT); - int buttonWidth = (width - SIDE_PANEL_SEPARATOR) / 2; - int buttonHeight = height - SIDE_PANEL_SEPARATOR - firstLineHeight; - float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; - if (fontSize > MAX_BUTTON_FONT_SIZE) - fontSize = MAX_BUTTON_FONT_SIZE; - - currentPlayerNameLabel.setBounds(x, y, width, firstLineHeight); - sortByGroupsButton.setBounds(x, y + firstLineHeight - + SIDE_PANEL_SEPARATOR, buttonWidth, buttonHeight); - sortByRunsButton.setBounds(x + buttonWidth + SIDE_PANEL_SEPARATOR, y - + firstLineHeight + SIDE_PANEL_SEPARATOR, buttonWidth, buttonHeight); - - sortByGroupsButton.setFont(sortByGroupsButton.getFont().deriveFont( - fontSize)); - sortByRunsButton.setFont(sortByRunsButton.getFont() - .deriveFont(fontSize)); - } - } - - private class RightPanelResizeListener extends ComponentAdapter { - @Override - public void componentResized(ComponentEvent e) { - Insets insets = rightPanel.getInsets(); - int x = insets.left, y = insets.top, width = rightPanel.getWidth() - - insets.left - insets.right, height = rightPanel.getHeight() - - insets.top - insets.bottom; - - if (width > SIDE_PANEL_MAX_WIDTH) { - x += (width - SIDE_PANEL_MAX_WIDTH) / 4; - width = width / 2 + SIDE_PANEL_MAX_WIDTH / 2; - } - - int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT); - int buttonWidth = width; - int buttonHeight = height - SIDE_PANEL_SEPARATOR - firstLineHeight; - float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; - if (fontSize > MAX_BUTTON_FONT_SIZE) - fontSize = MAX_BUTTON_FONT_SIZE; - - timeBar.setBounds(x, y, width, firstLineHeight); - endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR, - buttonWidth, buttonHeight); - endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize)); - } - } + private final static int SIDE_PANEL_INSET = 15; + private final static int SIDE_PANEL_SEPARATOR = 10; + private final static float SIDE_PANEL_FIRST_LINE_HEIGHT = 0.375f; + private final static int SIDE_PANEL_MAX_WIDTH = 180; + private final static float MAX_BUTTON_FONT_SIZE = 12; + + private final static DecimalFormat secondFormat = new DecimalFormat("00"); + + private HandPanel hand; + + private JPanel leftPanel, rightPanel; + + private JLabel currentPlayerNameLabel; + private JButton sortByGroupsButton; + private JButton sortByRunsButton; + private JProgressBar timeBar; + private JButton endTurnButton; + + private Event sortByGroupsEvent = new Event(); + private Event sortByRunsEvent = new Event(); + private Event endTurnEvent = new Event(); + + @Override + public HandPanel getHandPanel() { + return hand; + } + + void setCurrentPlayerName(String playerName) { + currentPlayerNameLabel.setText(playerName); + } + + @Override + public void setTimeLeft(int time) { + timeBar.setValue(time); + timeBar.setString(Integer.toString(time / 60) + ":" + + secondFormat.format(time % 60)); + } + + @Override + public IEvent getSortByGroupsEvent() { + return sortByGroupsEvent; + } + + @Override + public IEvent getSortByRunsEvent() { + return sortByRunsEvent; + } + + @Override + public IEvent getEndTurnEvent() { + return endTurnEvent; + } + + private void createLeftPanel() { + leftPanel = new JPanel(); + leftPanel.setLayout(null); + leftPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, + SIDE_PANEL_INSET, SIDE_PANEL_INSET)); + + currentPlayerNameLabel = new JLabel(); + currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER); + currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER); + currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER); + currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER); + leftPanel.add(currentPlayerNameLabel); + + sortByGroupsButton = new JButton("
Nach Gruppen sortieren"); + sortByGroupsButton.setFont(sortByGroupsButton.getFont().deriveFont(0)); + sortByGroupsButton.setMargin(new Insets(0, 0, 0, 0)); + sortByGroupsButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + sortByGroupsEvent.emit(); + } + }); + leftPanel.add(sortByGroupsButton); + + sortByRunsButton = new JButton("
Nach Reihen sortieren"); + sortByRunsButton.setFont(sortByRunsButton.getFont().deriveFont(0)); + sortByRunsButton.setMargin(new Insets(0, 0, 0, 0)); + sortByRunsButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + sortByRunsEvent.emit(); + } + }); + leftPanel.add(sortByRunsButton); + + leftPanel.addComponentListener(new LeftPanelResizeListener()); + } + + private void createRightPanel() { + rightPanel = new JPanel(); + rightPanel.setLayout(null); + rightPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, + SIDE_PANEL_INSET, SIDE_PANEL_INSET)); + + timeBar = new JProgressBar(0, 60); + timeBar.setStringPainted(true); + rightPanel.add(timeBar); + + endTurnButton = new JButton("Zug beenden"); + endTurnButton.setFont(endTurnButton.getFont().deriveFont(0)); + endTurnButton.setMargin(new Insets(0, 0, 0, 0)); + endTurnButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent arg0) { + endTurnEvent.emit(); + } + }); + + rightPanel.add(endTurnButton); + + rightPanel.addComponentListener(new RightPanelResizeListener()); + } + + 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; + int boardWidth = hand.getWidth(); + int panelWidth = (width - boardWidth) / 2; + + leftPanel.setBounds(x, y, panelWidth, height); + hand.setBounds(x + panelWidth, y, boardWidth, height); + rightPanel.setBounds(x + panelWidth + boardWidth, y, panelWidth, height); + + leftPanel.validate(); + rightPanel.validate(); + } + + /** + * Creates a new PlayerPanel instance + */ + PlayerPanel() { + setLayout(null); + + createLeftPanel(); + add(leftPanel); + + hand = new HandPanel(); + add(hand); + + createRightPanel(); + add(rightPanel); + + ComponentListener rescaleListener = new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + rescale(); + } + }; + + addComponentListener(rescaleListener); + hand.addComponentListener(rescaleListener); + } + + private class LeftPanelResizeListener extends ComponentAdapter { + @Override + public void componentResized(ComponentEvent e) { + Insets insets = leftPanel.getInsets(); + int x = insets.left, y = insets.top, width = leftPanel.getWidth() + - insets.left - insets.right, height = leftPanel.getHeight() + - insets.top - insets.bottom; + + if (width > SIDE_PANEL_MAX_WIDTH) { + x += (width - SIDE_PANEL_MAX_WIDTH) / 4; + width = width / 2 + SIDE_PANEL_MAX_WIDTH / 2; + } + + int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT); + int buttonWidth = (width - SIDE_PANEL_SEPARATOR) / 2; + int buttonHeight = height - SIDE_PANEL_SEPARATOR - firstLineHeight; + float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; + if (fontSize > MAX_BUTTON_FONT_SIZE) + fontSize = MAX_BUTTON_FONT_SIZE; + + currentPlayerNameLabel.setBounds(x, y, width, firstLineHeight); + sortByGroupsButton.setBounds(x, y + firstLineHeight + + SIDE_PANEL_SEPARATOR, buttonWidth, buttonHeight); + sortByRunsButton.setBounds(x + buttonWidth + SIDE_PANEL_SEPARATOR, y + + firstLineHeight + SIDE_PANEL_SEPARATOR, buttonWidth, buttonHeight); + + sortByGroupsButton.setFont(sortByGroupsButton.getFont().deriveFont( + fontSize)); + sortByRunsButton.setFont(sortByRunsButton.getFont().deriveFont(fontSize)); + } + } + + private class RightPanelResizeListener extends ComponentAdapter { + @Override + public void componentResized(ComponentEvent e) { + Insets insets = rightPanel.getInsets(); + int x = insets.left, y = insets.top, width = rightPanel.getWidth() + - insets.left - insets.right, height = rightPanel.getHeight() + - insets.top - insets.bottom; + + if (width > SIDE_PANEL_MAX_WIDTH) { + x += (width - SIDE_PANEL_MAX_WIDTH) / 4; + width = width / 2 + SIDE_PANEL_MAX_WIDTH / 2; + } + + int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT); + int buttonWidth = width; + int buttonHeight = height - SIDE_PANEL_SEPARATOR - firstLineHeight; + float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; + if (fontSize > MAX_BUTTON_FONT_SIZE) + fontSize = MAX_BUTTON_FONT_SIZE; + + timeBar.setBounds(x, y, width, firstLineHeight); + endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR, + buttonWidth, buttonHeight); + endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize)); + } + } } diff --git a/src/jrummikub/view/impl/StartTurnPanel.java b/src/jrummikub/view/impl/StartTurnPanel.java new file mode 100644 index 0000000..d52ce22 --- /dev/null +++ b/src/jrummikub/view/impl/StartTurnPanel.java @@ -0,0 +1,89 @@ +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; + +@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(); + + 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(); + } + }); + } + + void setCurrentPlayerName(String playerName) { + startTurnLabel.setText("'" + playerName + "' ist jetzt an der Reihe."); + } + + 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)); + } +} diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index 7ef0e18..e3d58ea 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -10,6 +10,7 @@ import javax.swing.JFrame; import javax.swing.border.MatteBorder; import jrummikub.model.Stone; +import jrummikub.util.IEvent; import jrummikub.view.IPlayerPanel; import jrummikub.view.ITablePanel; import jrummikub.view.IView; @@ -19,70 +20,95 @@ import jrummikub.view.IView; */ @SuppressWarnings("serial") public class View extends JFrame implements IView { - private TablePanel table; - private PlayerPanel playerPanel; - - private final static float PLAYER_PANEL_RATIO = 0.14f; - private final static int PLAYER_PANEL_BORDER_WIDTH = 1; - private final static int PLAYER_PANEL_MAX_HEIGHT = 180 + PLAYER_PANEL_BORDER_WIDTH; - - private static int even(double d) { - return 2 * (int) (d / 2); - } - - public ITablePanel getTablePanel() { - return table; - } - - public IPlayerPanel getPlayerPanel() { - return playerPanel; - } - - /** - * Create a new instance of the view - */ - public View() { - super("JRummikub"); - setLayout(null); - - setSize(800, 600); - setDefaultCloseOperation(EXIT_ON_CLOSE); - - table = new TablePanel(); - add(table); - - playerPanel = new PlayerPanel(); - playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0, - Color.BLACK)); - add(playerPanel); - - addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent e) { - Insets insets = getInsets(); - int width = getWidth() - insets.left - - insets.right, height = getHeight() - insets.top - insets.bottom; - - int playerPanelHeight = even(Math.pow((double) width * width * height, - 1 / 3.0) * PLAYER_PANEL_RATIO) - + PLAYER_PANEL_BORDER_WIDTH; - if (playerPanelHeight > PLAYER_PANEL_MAX_HEIGHT) - playerPanelHeight = PLAYER_PANEL_MAX_HEIGHT; - - int tableHeight = height - playerPanelHeight; - - table.setBounds(0, 0, width, tableHeight); - table.validate(); - playerPanel.setBounds(0, tableHeight, width, playerPanelHeight); - } - }); - - setVisible(true); - } - - @Override - public void setSelectedStones(Collection stones) { - table.setSelectedStones(stones); - playerPanel.getHandPanel().setSelectedStones(stones); - } + private TablePanel table; + private PlayerPanel playerPanel; + private StartTurnPanel startTurnPanel; + + private final static float PLAYER_PANEL_RATIO = 0.14f; + private final static int PLAYER_PANEL_BORDER_WIDTH = 1; + private final static int PLAYER_PANEL_MAX_HEIGHT = 180 + PLAYER_PANEL_BORDER_WIDTH; + + private static int even(double d) { + return 2 * (int) (d / 2); + } + + @Override + public ITablePanel getTablePanel() { + return table; + } + + @Override + public IPlayerPanel getPlayerPanel() { + return playerPanel; + } + + /** + * Create a new instance of the view + */ + public View() { + super("JRummikub"); + setLayout(null); + + setSize(800, 600); + setDefaultCloseOperation(EXIT_ON_CLOSE); + + table = new TablePanel(); + add(table); + + playerPanel = new PlayerPanel(); + playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0, + Color.BLACK)); + add(playerPanel); + + startTurnPanel = new StartTurnPanel(); + startTurnPanel.setVisible(false); + add(startTurnPanel); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + Insets insets = getInsets(); + int width = getWidth() - insets.left - insets.right, height = getHeight() + - insets.top - insets.bottom; + + int playerPanelHeight = even(Math.pow((double) width * width * height, + 1 / 3.0) * PLAYER_PANEL_RATIO) + + PLAYER_PANEL_BORDER_WIDTH; + if (playerPanelHeight > PLAYER_PANEL_MAX_HEIGHT) + playerPanelHeight = PLAYER_PANEL_MAX_HEIGHT; + + int tableHeight = height - playerPanelHeight; + + table.setBounds(0, 0, width, tableHeight); + table.validate(); + playerPanel.setBounds(0, tableHeight, width, playerPanelHeight); + startTurnPanel.setBounds(0, tableHeight, width, playerPanelHeight); + } + }); + + setVisible(true); + } + + @Override + public void setSelectedStones(Collection stones) { + table.setSelectedStones(stones); + playerPanel.getHandPanel().setSelectedStones(stones); + } + + @Override + public void enableStartTurnPanel(boolean enable) { + playerPanel.setVisible(!enable); + startTurnPanel.setVisible(enable); + } + + @Override + public void setCurrentPlayerName(String playerName) { + playerPanel.setCurrentPlayerName(playerName); + startTurnPanel.setCurrentPlayerName(playerName); + } + + @Override + public IEvent getStartTurnEvent() { + return startTurnPanel.getStartTurnEvent(); + } } -- cgit v1.2.3