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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
package jrummikub.view.impl;
import java.awt.Color;
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 java.awt.event.ComponentListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.view.IPlayerPanel;
/**
* Implementation of the player panel
*/
@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 HAND_ROW_BUTTON_RATIO = 0.03f;
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 JButton handRowUpButton;
private JButton handRowDownButton;
private JProgressBar timeBar;
private JButton endTurnButton;
private JButton keepStonesButton;
private JButton redealButton;
private Event sortByGroupsEvent = new Event();
private Event sortByRunsEvent = new Event();
private Event endTurnEvent = new Event();
private Event redealEvent = new Event();
HandPanel getHandPanel() {
return hand;
}
/**
* Sets the current player name
*
* @param playerName
* the player name
*/
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));
if (time <= 10)
timeBar.setForeground(Color.RED);
else
timeBar.setForeground(UIManager.getColor("ProgressBar.foreground"));
}
@Override
public IEvent getSortByGroupsEvent() {
return sortByGroupsEvent;
}
@Override
public IEvent getSortByRunsEvent() {
return sortByRunsEvent;
}
@Override
public IEvent getEndTurnEvent() {
return endTurnEvent;
}
@Override
public IEvent getRedealEvent() {
return redealEvent;
}
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 = createButton(leftPanel, "<html><center>Nach Sammlungen sortieren", new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
sortByGroupsEvent.emit();
}
});
sortByRunsButton = createButton(leftPanel, "<html><center>Nach Reihen sortieren", new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
sortByRunsEvent.emit();
}
});
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));
handRowUpButton = createButton(rightPanel, "<html><center>\u25B2", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hand.rowUp();
}
});
handRowDownButton = createButton(rightPanel, "<html><center>\u25BC", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hand.rowDown();
}
});
timeBar = new JProgressBar(0, 60);
timeBar.setStringPainted(true);
rightPanel.add(timeBar);
endTurnButton = createButton(rightPanel, "---", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endTurnEvent.emit();
}
});
keepStonesButton = createButton(rightPanel, "<html><center>Steine<br>behalten", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endTurnEvent.emit();
}
});
redealButton = createButton(rightPanel, "<html><center>Neu<br>geben", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redealEvent.emit();
}
});
rightPanel.addComponentListener(new RightPanelResizeListener());
}
private JButton createButton(JPanel panel, String caption, ActionListener listener) {
JButton button = new JButton(caption);
button.setFont(button.getFont().deriveFont(0));
button.setMargin(new Insets(0, 0, 0, 0));
button.addActionListener(listener);
panel.add(button);
return button;
}
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 handButtonWidth = (int) (width * HAND_ROW_BUTTON_RATIO);
int meanPanelWidth = (width - boardWidth) / 2;
int leftPanelWidth = meanPanelWidth - handButtonWidth / 2;
int rightPanelWidth = meanPanelWidth + handButtonWidth / 2;
leftPanel.setBounds(x, y, leftPanelWidth, height);
hand.setBounds(x + leftPanelWidth, y, boardWidth, height);
rightPanel.setBounds(x + leftPanelWidth + boardWidth, y, rightPanelWidth,
height);
leftPanel.validate();
rightPanel.validate();
}
void updateButtons() {
handRowUpButton.setEnabled(hand.canRowUp());
handRowUpButton.setForeground(hand.canRowUp() ? Color.BLACK : Color.GRAY);
handRowDownButton.setEnabled(hand.canRowDown());
handRowDownButton.setForeground(hand.canRowDown() ? Color.BLACK
: Color.GRAY);
}
/**
* Creates a new PlayerPanel instance
*/
PlayerPanel() {
setLayout(null);
createLeftPanel();
add(leftPanel);
hand = new HandPanel(this);
add(hand);
createRightPanel();
add(rightPanel);
ComponentListener rescaleListener = new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
};
addComponentListener(rescaleListener);
hand.addComponentListener(rescaleListener);
setEndTurnMode(true, true);
}
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) / 6;
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) {
int handButtonWidth = (int) (getWidth() * HAND_ROW_BUTTON_RATIO);
Insets insets = rightPanel.getInsets();
int x = insets.left + handButtonWidth, y = insets.top, width = rightPanel
.getWidth() - insets.left - insets.right - handButtonWidth, 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 smallButtonWidth = (width - SIDE_PANEL_SEPARATOR) / 2;
int buttonHeight = height - SIDE_PANEL_SEPARATOR - firstLineHeight;
float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5;
float smallFontSize = (float) Math.sqrt(smallButtonWidth * buttonHeight) / 5;
if (fontSize > MAX_BUTTON_FONT_SIZE)
fontSize = MAX_BUTTON_FONT_SIZE;
handRowUpButton.setBounds(0, 0, handButtonWidth, getHeight() / 2);
handRowUpButton.setFont(handRowUpButton.getFont().deriveFont(
fontSize * 1.5f));
handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth,
getHeight() / 2);
handRowDownButton.setFont(handRowDownButton.getFont().deriveFont(
fontSize * 1.5f));
timeBar.setBounds(x, y, width, firstLineHeight);
endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR,
buttonWidth, buttonHeight);
endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize));
redealButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR, smallButtonWidth, buttonHeight);
redealButton.setFont(redealButton.getFont().deriveFont(smallFontSize));
keepStonesButton.setBounds(x + smallButtonWidth + SIDE_PANEL_SEPARATOR, y
+ firstLineHeight + SIDE_PANEL_SEPARATOR, smallButtonWidth, buttonHeight);
keepStonesButton.setFont(keepStonesButton.getFont().deriveFont(smallFontSize));
}
}
public void setEndTurnMode(boolean inspectOnly, boolean mayRedeal) {
if (!inspectOnly) {
endTurnButton.setText("Zug beenden");
} else if (!mayRedeal) {
endTurnButton.setText("Nächster Spieler");
}
boolean smallButtons = mayRedeal && inspectOnly;
endTurnButton.setVisible(!smallButtons);
keepStonesButton.setVisible(smallButtons);
redealButton.setVisible(smallButtons);
}
}
|