1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
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.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
import jrummikub.model.Stone;
import jrummikub.util.IEvent;
import jrummikub.view.IHandPanel;
import jrummikub.view.IPlayerPanel;
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 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 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));
playerPanel.setVisible(false);
mainLayer.add(playerPanel);
startTurnPanel = new StartTurnPanel();
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);
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);
}
@Override
public void setSelectedStones(Collection<Stone> 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 setCurrentPlayerName(String playerName) {
playerPanel.setCurrentPlayerName(playerName);
startTurnPanel.setCurrentPlayerName(playerName);
winPanel.setCurrentPlayerName(playerName);
}
@Override
public IEvent getStartTurnEvent() {
return startTurnPanel.getStartTurnEvent();
}
@Override
public IEvent getNewRoundEvent() {
return winPanel.getNewRoundEvent();
}
@Override
public IEvent getFinalScoreEvent() {
return winPanel.getFinalScoreEvent();
}
}
|