package jrummikub.view.impl; import java.awt.Color; import java.awt.Insets; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Collection; 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; /** * Implementation of the top-level view interface */ @SuppressWarnings("serial") public class View extends JFrame implements IView { private TablePanel table; private PlayerPanel playerPanel; private StartTurnPanel startTurnPanel; private WinPanel winPanel; 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); winPanel = new WinPanel(); winPanel.setVisible(false); add(winPanel); 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); winPanel.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); winPanel.setVisible(false); } @Override public void enableWinPanel(boolean enable) { playerPanel.setVisible(!enable); startTurnPanel.setVisible(false); winPanel.setVisible(enable); } @Override public void setCurrentPlayerName(String playerName) { playerPanel.setCurrentPlayerName(playerName); startTurnPanel.setCurrentPlayerName(playerName); } @Override public IEvent getStartTurnEvent() { return startTurnPanel.getStartTurnEvent(); } @Override public IEvent getNewGameEvent() { return winPanel.getNewGameEvent(); } @Override public IEvent getQuitEvent() { return winPanel.getQuitEvent(); } }