-
Notifications
You must be signed in to change notification settings - Fork 1
/
LaneView.java
240 lines (191 loc) · 6.11 KB
/
LaneView.java
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
/*
* constructs a prototype Lane View
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
public class LaneView implements LaneObserver {
private int roll;
private boolean initDone = true;
JFrame frame;
Container cpanel;
Vector<Bowler> bowlers;
int cur;
Iterator<Bowler> bowlIt;
JPanel[][] balls;
JLabel[][] ballLabel;
JPanel[][] scores;
JLabel[][] scoreLabel;
JPanel[][] ballGrid;
JPanel[] pins;
JButton maintenance;
Lane lane;
private ButtonCommand command; // the Command instance for Command pattern
public LaneView(Lane lane, int laneNum) {
this.lane = lane;
initDone = true;
frame = new JFrame("Lane " + laneNum + ":");
cpanel = frame.getContentPane();
cpanel.setLayout(new BorderLayout());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.setVisible(false);
}
});
cpanel.add(new JPanel());
}
public void show() {
frame.setVisible(true);
}
public void hide() {
frame.setVisible(false);
}
private JPanel makeFrame(Party party) {
initDone = false;
bowlers = party.getMembers();
int numBowlers = bowlers.size();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
balls = new JPanel[numBowlers][23];
ballLabel = new JLabel[numBowlers][23];
scores = new JPanel[numBowlers][10];
scoreLabel = new JLabel[numBowlers][10];
ballGrid = new JPanel[numBowlers][10];
pins = new JPanel[numBowlers];
for (int i = 0; i != numBowlers; i++) {
for (int j = 0; j != 23; j++) {
ballLabel[i][j] = new JLabel(" ");
balls[i][j] = new JPanel();
balls[i][j].setBorder(
BorderFactory.createLineBorder(Color.BLACK));
balls[i][j].add(ballLabel[i][j]);
}
}
for (int i = 0; i != numBowlers; i++) {
for (int j = 0; j != 9; j++) {
ballGrid[i][j] = new JPanel();
ballGrid[i][j].setLayout(new GridLayout(0, 3));
ballGrid[i][j].add(new JLabel(" "), BorderLayout.EAST);
ballGrid[i][j].add(balls[i][2 * j], BorderLayout.EAST);
ballGrid[i][j].add(balls[i][2 * j + 1], BorderLayout.EAST);
}
int j = 9;
ballGrid[i][j] = new JPanel();
ballGrid[i][j].setLayout(new GridLayout(0, 3));
ballGrid[i][j].add(balls[i][2 * j]);
ballGrid[i][j].add(balls[i][2 * j + 1]);
ballGrid[i][j].add(balls[i][2 * j + 2]);
}
for (int i = 0; i != numBowlers; i++) {
pins[i] = new JPanel();
pins[i].setBorder(
BorderFactory.createTitledBorder(
((Bowler) bowlers.get(i)).getNickName()));
pins[i].setLayout(new GridLayout(0, 10));
for (int k = 0; k != 10; k++) {
scores[i][k] = new JPanel();
scoreLabel[i][k] = new JLabel(" ", SwingConstants.CENTER);
scores[i][k].setBorder(
BorderFactory.createLineBorder(Color.BLACK));
scores[i][k].setLayout(new GridLayout(0, 1));
scores[i][k].add(ballGrid[i][k], BorderLayout.EAST);
scores[i][k].add(scoreLabel[i][k], BorderLayout.SOUTH);
pins[i].add(scores[i][k], BorderLayout.EAST);
}
panel.add(pins[i]);
}
initDone = true;
return panel;
}
public void receiveLaneEvent(LaneEvent le) { // 레인 이벤트를 처리하는 메소드 리팩토링
if (lane.isPartyAssigned()) {
int numBowlers = le.getParty().getMembers().size();
waitDoSleep();
if (isFirstFrameFirstBall(le)) {
setupFrame(le);
}
int[][] lescores = le.getCumulScore();
updateScoreLabels(numBowlers, lescores, le);
}
}
private void waitDoSleep(){ // 초기화 대기 메소드
while (!initDone) {
//System.out.println("chillin' here.");
try {
Thread.sleep(1);
} catch (Exception e) {
}
}
}
private boolean isFirstFrameFirstBall(LaneEvent le) { // 첫번째 프레임의 시작인지 확인 메소드
return le.getFrameNum() == 1 && le.getBall() == 0 && le.getIndex() == 0;
}
private void setupFrame(LaneEvent le){ // 프레임을 초기화하는 메소드
LaneViewClickEvent listener = new LaneViewClickEvent();
System.out.println("Making the frame.");
cpanel.removeAll();
cpanel.add(makeFrame(le.getParty()), "Center");
// Button Panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
maintenance = createButton("Maintenance Call", listener);
buttonPanel.add(maintenance);
cpanel.add(buttonPanel, "South");
frame.pack();
}
private JButton createButton(String text, LaneViewClickEvent listener) {
JButton button = new JButton(text);
button.addActionListener(listener);
return button;
}
private void updateScoreLabels(int numBowlers, int[][] lescores, LaneEvent le){ // 점수와 라벨을 업데이트
for (int k = 0; k < numBowlers; k++) {
updateCumlativeScores(lescores, le.getFrameNum(), k);
updateBallScores(le, k);
}
}
private void updateCumlativeScores(int[][] lescores, int frameNum, int k){ // 누적 점수 업데이트 메소드
for (int i = 0; i <= frameNum - 1; i++) {
if (lescores[k][i] != 0)
scoreLabel[k][i].setText(String.valueOf(lescores[k][i]));
}
}
private void updateBallScores(LaneEvent le, int k){ // 점수에 대해서 전략 패턴 적용 => 추후 다른 점수 규칙 도입에 용
List<ScoringStrategy> strategies = Arrays.asList(
new StrikeScoringStrategy(),
new SpareScoringStrategy(),
new FoulScoringStrategy(),
new DefaultScoringStrategy());
for (int i = 0; i < 21; i++) {
int[] bowlerScores = (le.getScore()).get(bowlers.get(k));
if (bowlerScores[i] != -1) {
for (ScoringStrategy strategy : strategies) { // 4가지 유형 중 하나임.
String scoreSymbol = strategy.getScoreSymbol(bowlerScores, i);
if (scoreSymbol != null) {
ballLabel[k][i].setText(scoreSymbol);
break;
}
}
}
}
}
public class LaneViewClickEvent implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(maintenance)) {
setCommand(new LaneViewMaintenanceCommand(LaneView.this));
}
buttonPressed();
}
}
// The Invoker holds a command and can use it to call a method
public void setCommand(ButtonCommand command) {
this.command = command;
}
// Call the execute() method of the command
public void buttonPressed() {
command.execute();
}
}