-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameView.java
364 lines (344 loc) · 13.7 KB
/
GameView.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.lang.Thread;
public class GameView implements ActionListener {
// 棋盘的总行数和总列数
private Game game;
private int width = 800;
private int height = 800;
private int x = 800;
private int y = 150;
int cnt = 0;
// 主面板
private JFrame mainFrame;
private Container thisContainer;
// 子面板
private JPanel centerPanel, southPanel, northPanel;
// 重列,重新开始按钮
private JButton resetButton, newlyButton, saveButton, loadButton, webButton, loadplaybackButton, loadfromwebButton,
loadplayfromwebButton;
// 界面按钮数组,和棋盘数组 一样大
private JButton[][] allButton;
private Image images[];
// 初始化界面
public void init() {
game = new Game();
game.createNewGame();
mainFrame = new JFrame("连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
thisContainer.add(centerPanel, "Center");
thisContainer.add(southPanel, "South");
thisContainer.add(northPanel, "North");
centerPanel.setLayout(new GridLayout(game.rows + 2, game.columns + 2));
images = new Image[game.imgs + 1];
for (int i = 1; i <= game.imgs; ++i) {
images[i] = new ImageIcon("source/" + i + ".png").getImage();
}
this.allButton = new JButton[game.rows + 2][game.columns + 2];
for (int i = 0; i <= game.rows + 1; i++) {
for (int j = 0; j <= game.columns + 1; j++) {
allButton[i][j] = new JButton();
allButton[i][j].addActionListener(this);
allButton[i][j].setBackground(Color.WHITE);
centerPanel.add(allButton[i][j]);
}
}
resetButton = new JButton("重列");
resetButton.addActionListener(this);
newlyButton = new JButton("再来一局");
newlyButton.addActionListener(this);
saveButton = new JButton("存储游戏");
saveButton.addActionListener(this);
loadButton = new JButton("导入游戏");
loadButton.addActionListener(this);
loadplaybackButton = new JButton("导入回放");
loadplaybackButton.addActionListener(this);
webButton = new JButton("上传到网络");
webButton.addActionListener(this);
loadfromwebButton = new JButton("从网络导入游戏");
loadfromwebButton.addActionListener(this);
loadplayfromwebButton = new JButton("从网络导入回放");
loadplayfromwebButton.addActionListener(this);
southPanel.add(resetButton);
southPanel.add(newlyButton);
southPanel.add(saveButton);
southPanel.add(loadButton);
southPanel.add(loadplaybackButton);
southPanel.add(webButton);
southPanel.add(loadfromwebButton);
southPanel.add(loadplayfromwebButton);
mainFrame.setBounds(x, y, width, height);
mainFrame.setVisible(true);
centerPanel.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
drawBoard(game.x1, game.y1);
}
});
drawBoard(0, 0);
}
public void operation(int x, int y) {
for (int i = 0; i <= game.rows + 1; i++) {
for (int j = 0; j <= game.columns + 1; j++) {
System.out.print(game.board[i][j]);
System.out.print(" ");
}
System.out.println("");
}
System.out.println(x + " " + y);
Map<String, Object> res = game.canDelete(x, y);
if ((boolean) res.get("result") == true) {
List<Position> lis = (List<Position>) res.get("list");
drawLine(lis);
}
game.press(x, y);
drawBoard(x, y);
boolean win = true;
for (int i = 1; i <= game.rows; i++) {
for (int j = 1; j <= game.rows; j++) {
if (game.board[i][j] != 0) {
win = false;
break;
}
}
}
if (win) {
JDialog dialog = new JDialog(mainFrame);
dialog.setSize(200, 200);
dialog.setLocation(500, 500);
dialog.setLayout(new BorderLayout());
dialog.add(new JLabel("您赢了!", JLabel.CENTER));
dialog.setVisible(true);
}
}
public void drawLine(List<Position> list) {
int width = centerPanel.getWidth();
int height = centerPanel.getHeight();
for (int i = 0; i < list.size() - 1; i++) {
// 最后一个点没有下个连接点
Position position1 = list.get(i);
Position position2 = list.get(i + 1);
int x1 = position1.getX();
int y1 = position1.getY();
int x2 = position2.getX();
int y2 = position2.getY();
double x3 = (double) width / (game.columns + 2) * (y1 + 0.5);
double y3 = (double) height / (game.rows + 2) * (x1 + 0.5);
double x4 = (double) width / (game.columns + 2) * (y2 + 0.5);
double y4 = (double) height / (game.rows + 2) * (x2 + 0.5);
Graphics2D graphics2D = (Graphics2D) centerPanel.getGraphics();
graphics2D.setColor(new Color(255, 117, 0));
Line2D line2D = new Line2D.Double(x3, y3, x4, y4);
graphics2D.draw(line2D);
System.err.println(x1 + " " + y1 + " " + x2 + " " + y2);
}
try {
Thread.sleep(300);
} catch (Exception e) {
}
centerPanel.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
// 如果是新开一局
if (e.getSource() == newlyButton) {
game.createNewGame();
drawBoard(0, 0);
}
// 重新布局剩余的图片
if (e.getSource() == resetButton) {
game.reset();
drawBoard(0, 0);
}
// 导入游戏
if (e.getSource() == loadButton) {
javax.swing.filechooser.FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("./data"));
JFileChooser fileChooser = new JFileChooser(fsv);
fileChooser.setCurrentDirectory(new File("./data"));
fileChooser.showOpenDialog(mainFrame);
File f = fileChooser.getSelectedFile();
try {
game = Game.readGameFromStream(new FileInputStream(f));
drawBoard(0, 0);
} catch (Exception ee) {
System.out.println(ee);
}
}
// 导入回放
if (e.getSource() == loadplaybackButton) {
javax.swing.filechooser.FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("./data"));
JFileChooser fileChooser = new JFileChooser(fsv);
fileChooser.setCurrentDirectory(new File("./data"));
fileChooser.showOpenDialog(mainFrame);
File f = fileChooser.getSelectedFile();
try {
game = Game.readGameFromStream(new FileInputStream(f));
replay();
} catch (Exception ee) {
System.out.println(ee);
}
}
// 存储游戏
if (e.getSource() == saveButton) {
javax.swing.filechooser.FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("./data"));
JFileChooser fileChooser = new JFileChooser(fsv);
fileChooser.setCurrentDirectory(new File("./data"));
fileChooser.showSaveDialog(mainFrame);
File f = fileChooser.getSelectedFile();
try {
game.saveGameToStream(new FileOutputStream(f));
} catch (IOException ee) {
System.out.println(ee);
}
}
// 存储到网络
if (e.getSource() == webButton) {
cnt++;
Client c = new Client();
File f = new File(c.dir + c.sendname);
try {
game.saveGameToStream(new FileOutputStream(f));
c.sendFile(f.getAbsolutePath());
} catch (IOException ee) {
System.out.println("get IOExecption when sending file. ");
}
}
// 从网络导入游戏
if (e.getSource() == loadfromwebButton) {
try {
Client c = new Client();
String[] names = c.getList();
selectFileAndLoad(names, false);
} catch (IOException ee) {
System.out.println("get IOExecption when getting file. ");
} catch (Exception ee) {
System.out.println(ee);
}
}
// 从网络导入回放
if (e.getSource() == loadplayfromwebButton) {
try {
Client c = new Client();
String[] names = c.getList();
selectFileAndLoad(names, true);
} catch (IOException ee) {
System.out.println("get IOExecption when getting file. ");
} catch (Exception ee) {
System.out.println(ee);
}
}
// 监控棋盘
for (int i = 1; i <= game.rows; i++) {
for (int j = 1; j <= game.columns; j++) {
if (e.getSource() == allButton[i][j]) {
System.out.println(i + " " + j);
operation(i, j);
}
}
}
}
public void drawBoard(int x, int y) {
int iconSize = Math.min(centerPanel.getWidth() / (game.columns + 2), centerPanel.getHeight() / (game.rows + 2))
* 3 / 5;
System.out.println(centerPanel.getSize());
for (int i = 0; i <= game.rows + 1; i++) {
for (int j = 0; j <= game.columns + 1; j++) {
// allButton[i][j].setText(game.board[i][j] + "");
allButton[i][j].setText("");
if (game.board[i][j] != 0) {
Image newimg = images[game.board[i][j]].getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
allButton[i][j].setIcon(new ImageIcon(newimg));
}
allButton[i][j].setVisible(game.board[i][j] != 0);
allButton[i][j].setFocusPainted(game.pressed && i == x && j == y);
}
}
centerPanel.repaint();
}
public void replay() {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i <= game.rows + 1; i++) {
for (int j = 0; j <= game.columns + 1; j++) {
game.board[i][j] = game.iniboard[i][j];
}
}
List<Position> history = game.history;
game.history = new ArrayList<Position>();
game.pressed = false;
game.x1 = game.y1 = 0;
drawBoard(0, 0);
try {
Thread.sleep(300);
} catch (Exception e) {
}
for (int i = 0; i < history.size(); i++) {
int x = history.get(i).getX(), y = history.get(i).getY();
operation(x, y);
allButton[6][6].setFocusPainted(true);
centerPanel.repaint();
try {
Thread.sleep(300);
} catch (Exception e) {
}
}
}
}).start();
}
public void selectFileAndLoad(String[] names, Boolean replay) throws IOException {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 300, 160);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null); // 窗体居中
frame.setVisible(true);
JComboBox comboBox = new JComboBox();
// 下拉框
for (String item : names) {
comboBox.addItem(item);
}
comboBox.setFont(new Font("宋体", Font.PLAIN, 14));
comboBox.setBounds(80, 20, 140, 23);
frame.getContentPane().add(comboBox);
JButton btnComfirm = new JButton("确定");
btnComfirm.setFont(new Font("宋体", Font.PLAIN, 14));
btnComfirm.setBounds(100, 80, 100, 25);
frame.getContentPane().add(btnComfirm);
btnComfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String filename = comboBox.getSelectedItem().toString();
Client c = new Client();
c.getFile(filename);
System.out.println("获取文件成功!");
File f = new File(c.dir + c.name);
game = Game.readGameFromStream(new FileInputStream(f));
if (!replay) {
drawBoard(0, 0);
} else {
replay();
}
frame.dispose();
} catch (IOException ee) {
ee.printStackTrace();
System.out.println("存入文件失败!");
} catch (Exception ee) {
ee.printStackTrace();
System.out.println("存入文件失败!");
}
}
});
}
}