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.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JPanel; import javax.swing.border.MatteBorder; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.util.IEvent; import jrummikub.util.Pair; import jrummikub.view.IHandPanel; import jrummikub.view.IPlayerPanel; import jrummikub.view.IScorePanel; import jrummikub.view.ISettingsPanel; 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 JLayeredPane layeredPane; private JPanel mainLayer; private TablePanel table; private PlayerPanel playerPanel; private StartTurnPanel startTurnPanel; private WinPanel winPanel; private SettingsPanel settingsPanel; private ScorePanel scorePanel; 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 ISettingsPanel getSettingsPanel() { return settingsPanel; } @Override public IScorePanel getScorePanel() { return scorePanel; } @Override public ITablePanel getTablePanel() { return table; } @Override public IHandPanel getHandPanel() { return playerPanel.getHandPanel(); } @Override public IPlayerPanel getPlayerPanel() { return playerPanel; } /** * Create a new instance of the view */ public View() { super("JRummikub"); setLayout(null); setSize(1000, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); layeredPane = new JLayeredPane(); layeredPane.setLayout(null); add(layeredPane); mainLayer = new JPanel(); mainLayer.setLayout(null); layeredPane.add(mainLayer); table = new TablePanel(); mainLayer.add(table); playerPanel = new PlayerPanel(); playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0, Color.BLACK)); mainLayer.add(playerPanel); startTurnPanel = new StartTurnPanel(); startTurnPanel.setVisible(false); mainLayer.add(startTurnPanel); winPanel = new WinPanel(); winPanel.setVisible(false); mainLayer.add(winPanel); settingsPanel = new SettingsPanel(); settingsPanel.setVisible(false); layeredPane.setLayer(settingsPanel, JLayeredPane.POPUP_LAYER); layeredPane.add(settingsPanel); scorePanel = new ScorePanel(); scorePanel.setVisible(false); layeredPane.setLayer(scorePanel, JLayeredPane.POPUP_LAYER); layeredPane.add(scorePanel); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { rescale(); } }); setVisible(true); } private void rescale() { Insets insets = getInsets(); int width = getWidth() - insets.left - insets.right, height = getHeight() - insets.top - insets.bottom; layeredPane.setBounds(0, 0, width, height); mainLayer.setBounds(0, 0, width, height); 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); settingsPanel.setBounds(width / 4, height / 4, width / 2, height / 2); scorePanel.setBounds(width / 8, height / 4, width * 3 / 4, height / 2); } @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 showSettingsPanel(boolean show) { settingsPanel.setVisible(show); } @Override public void showScorePanel(boolean show) { scorePanel.setVisible(show); } @Override public void setCurrentPlayerName(String playerName) { playerPanel.setCurrentPlayerName(playerName); startTurnPanel.setCurrentPlayerName(playerName); winPanel.setCurrentPlayerName(playerName); } @Override public void setCurrentPlayerColor(Color color) { playerPanel.setCurrentPlayerColor(color); } @Override public void setHasLaidOut(boolean hasLaidOut) { playerPanel.setHasLaidOut(hasLaidOut); } @Override public IEvent getStartTurnEvent() { return startTurnPanel.getStartTurnEvent(); } @Override public IEvent getNewRoundEvent() { return winPanel.getNewRoundEvent(); } @Override public IEvent getNewGameEvent() { return winPanel.getNewGameEvent(); } @Override public IEvent getEndProgramEvent() { return winPanel.getEndProgramEvent(); } @SuppressWarnings("unchecked") private List> createDecorationStones() { Pair stone1 = new Pair(new Stone( StoneColor.RED), new Position(3.5f, 0)); Pair stone2 = new Pair(new Stone(13, StoneColor.BLACK), new Position(5, 0)); Pair stone3 = new Pair(new Stone(13, StoneColor.ORANGE), new Position(6, 0)); Pair stone4 = new Pair(new Stone(13, StoneColor.BLUE), new Position(7, 0)); Pair stone5 = new Pair(new Stone(13, StoneColor.RED), new Position(8, 0)); Pair stone6 = new Pair(new Stone( StoneColor.BLACK), new Position(9.5f, 0)); return Arrays.asList(stone1, stone2, stone3, stone4, stone5, stone6); } @Override public void showInterface(boolean enable) { if (enable) { playerPanel.showButtons(true); } else { List> emptyTable = Collections.emptyList(); table.setStoneSets(emptyTable); playerPanel.getHandPanel().setStones(createDecorationStones()); playerPanel.showButtons(false); } } @Override public void enableThinkPanel(boolean enable) { playerPanel.enableButtons(!enable); } }