-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGameFrame.java
More file actions
532 lines (381 loc) · 16.6 KB
/
ChessGameFrame.java
File metadata and controls
532 lines (381 loc) · 16.6 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package view;
import controller.GameController;
import model.ChessColor;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static java.lang.Thread.sleep;
/**
* 这个类表示游戏过程中的整个游戏界面,是一切的载体
*/
public class ChessGameFrame extends JFrame {
public int cc=0;
// public final Dimension FRAME_SIZE ;
private final int WIDTH;
private final int HEIGTH;
public final int CHESSBOARD_SIZE;
private GameController gameController;
private JLabel jb;
public ChessGameFrame(int width, int height) {
setTitle("2022 CS102A Project Demo"); //设置标题
this.WIDTH = width;
this.HEIGTH = height;
this.CHESSBOARD_SIZE = HEIGTH * 4 / 5;
// int fraWidth=this.getWidth();
// int fraHeight=this.getHeight();
// Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// int scWidth=screenSize.width;
// int scHight=screenSize.height;
//// this.setSize(scWidth,scHight);
//
// float proportionW=scWidth/fraWidth;
// float proportionH=scHight/fraHeight;
//
//
// FrameShow.modifyComponentSize(this, proportionW,proportionH);
// this.toFront();
setSize(WIDTH, HEIGTH);
setLocationRelativeTo(null); // Center the window.
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置程序关闭按键,如果点击右上方的叉就游戏全部关闭了
setLayout(null);
addChangeBackground();
addChessboard();
ImageIcon imag=new ImageIcon("./images/21.png");
// JLabel jb=new JLabel(imag);
//
// jb.setBounds(0,0,getWidth(),getHeight());
//
// add(jb);
Image image=imag.getImage();
Image doSomImage=image.getScaledInstance(WIDTH,HEIGTH,Image.SCALE_FAST);
ImageIcon tureBackground = new ImageIcon(doSomImage);
jb=new JLabel(tureBackground);
jb.setBounds(0,0,getWidth(),getHeight());
add(jb);
// String file = "F:\\music.mp3";
// Music play = new Music(file);
// // 开启
// play.start();
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("stop!");
}
/**
* 在游戏面板中添加棋盘
*/
private void addChessboard() {
Chessboard chessboard = new Chessboard(CHESSBOARD_SIZE, CHESSBOARD_SIZE);
gameController = new GameController(chessboard);
chessboard.setLocation(HEIGTH / 10, HEIGTH / 10);
add(chessboard);
/**
* 在游戏面板中添加标签
*/
JLabel jb=new JLabel("Round: 0");
jb.setLocation(HEIGTH,HEIGTH/10-9);
jb.setSize(200,40);
jb.setFont(new Font("Rockwell", Font.BOLD, 25));
jb.setForeground(chessboard.getColor());
add(jb);
chessboard.setJb(jb);
JLabel statusLabel = new JLabel("Turn For White");
statusLabel.setLocation(HEIGTH, HEIGTH / 10);
statusLabel.setSize(200, 60);
statusLabel.setFont(new Font("Rockwell", Font.BOLD, 25));
statusLabel.setForeground(chessboard.getColor());
add(statusLabel);
chessboard.setLable(statusLabel);
JButton button = new JButton("Restart");
button.setLocation(HEIGTH, HEIGTH / 10 + 240);
button.setSize(200, 60);
button.setFont(new Font("Rockwell", Font.BOLD, 20));
add(button);
button.addActionListener(e -> {
if (chessboard.avoidBugChessOnChick()==false){
JOptionPane.showMessageDialog(null,"请先取消选中");
}
else
{int n=JOptionPane.showConfirmDialog(null,"Are you sure to restart","Confirm",JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION){
// chessboard.stopClick();
chessboard.setCurrentColor(ChessColor.WHITE);
chessboard.Restarted();
// System.out.printf("%d",chessboard.getA());
repaint();
}}
});
/**
* 在游戏面板中增加一个按钮,如果按下的话就会显示Hello, world!
*/
JButton button1 = new JButton("Store");
button1.setLocation(HEIGTH, HEIGTH / 10 + 80);
button1.setSize(200, 60);
button1.setFont(new Font("Rockwell", Font.BOLD, 20));
add(button1);
button1.addActionListener(e -> {
System.out.println("Click store");
int n=JOptionPane.showConfirmDialog(null,"Are you sure to store","Confirm",JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION){
System.out.println(chessboard.theStore());
String a=chessboard.theStore();
// saveAsFileWriter(chessboard.theStore());
// JFileChooser fileChooser = new JFileChooser();
////后缀名过滤器
// FileNameExtensionFilter filter = new FileNameExtensionFilter("标签文件(*.txt)", "txt");
// fileChooser.setFileFilter(filter);
//// 在容器上打开文件选择器
// fileChooser.showSaveDialog(fileChooser);
// File f=fileChooser.getSelectedFile();
////字节输出流
// FileOutputStream fos = null;
// try {
// String fname = f.getName(); //从文件名输入框中获取文件名
//
//
//
// //创建文件
// File file=new File(fileChooser.getCurrentDirectory()+"/"+fname+".txt");
// fos = new FileOutputStream(file);
// //写入文件操作
// String Datas = chessboard.theStore();
// fos.write(Datas.getBytes());
// fos.close();
//
// } catch (IOException e1) {
// System.err.println("IO异常");
// e1.printStackTrace();
// }
saveFile(a);
}
});
JButton buttonLoad = new JButton("Load");
buttonLoad.setLocation(HEIGTH, HEIGTH / 10 + 160);
buttonLoad.setSize(200, 60);
buttonLoad.setFont(new Font("Rockwell", Font.BOLD, 20));
add(buttonLoad);
// buttonLoad.addActionListener(e -> {
// System.out.println("Click load");
// String path = JOptionPane.showInputDialog(this,"Input Path here"); E://xxx.txt
// boolean pathForm=true;
//
// if ((path.charAt(path.length()-3)!='t')||(path.charAt(path.length()-2)!='x')||(path.charAt(path.length()-1)!='t')){
// pathForm=false;
// }
//
//
// gameController.loadGameFromFile(path);
// if(chessboard.isLoadTest()&&pathForm){
//
// repaint();
// }
// else {
// JOptionPane.showMessageDialog(null, "the message is not correct!", "Warnings", JOptionPane.WARNING_MESSAGE);
// }
// });
buttonLoad.addActionListener(e->{//csdn
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false); // 设为多选
int returnVal = chooser.showOpenDialog(buttonLoad); // 是否打开文件选择框,选择保存是0,退出和1
// System.out.println("returnVal=" + returnVal);
if (returnVal == JFileChooser.APPROVE_OPTION) { //点击确定
boolean pathForm=true;
String filepath = chooser.getSelectedFile().getAbsolutePath();//选择路径
// System.out.println(filepath);
//
// System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
String path =filepath;
// System.out.println(path);// 输出相对路径
if ((path.charAt(path.length()-3)!='t')||(path.charAt(path.length()-2)!='x')||(path.charAt(path.length()-1)!='t')){
pathForm=false;
JOptionPane.showMessageDialog(null, "文件格式错误(错误编码:104)", "Warnings", JOptionPane.WARNING_MESSAGE);
}
else {
gameController.loadGameFromFile(path);
if(chessboard.isLoadTest()&&pathForm){
repaint();
}
else {
if (!chessboard.isTheILength()&&chessboard.isTheChessName()&&chessboard.isTheInt()){
JOptionPane.showMessageDialog(null, "棋盘并非8*8", "Warnings", JOptionPane.WARNING_MESSAGE);
repaint();
System.out.println(chessboard.isTheChessName());
System.out.println(chessboard.isTheChessName());
System.out.println(chessboard.isTheInt());
}
else if (!chessboard.isTheChessName()&&chessboard.isTheILength()&&chessboard.isTheInt()){
JOptionPane.showMessageDialog(null, "棋子并非六种之一,棋子并非黑白棋子", "Warnings", JOptionPane.WARNING_MESSAGE);
repaint(); }
else if (!chessboard.isTheInt()&&chessboard.isTheChessName()&&chessboard.isTheILength()){
JOptionPane.showMessageDialog(null, "缺少行棋方", "Warnings", JOptionPane.WARNING_MESSAGE);
repaint();}
else{
JOptionPane.showMessageDialog(null, "多处存在问题", "Warnings", JOptionPane.WARNING_MESSAGE);
repaint();}
}
}}
});
JButton huiqi=new JButton("HuiQi");
huiqi.setLocation(HEIGTH, HEIGTH / 10 + 400);
huiqi.setSize(200, 60);
huiqi.setFont(new Font("Rockwell", Font.BOLD, 20));
add(huiqi);
huiqi.addActionListener(e -> {
if (chessboard.getIntStoreHuiQI()<2){
JOptionPane.showMessageDialog(null, "已经是开始状态", "Warnings", JOptionPane.WARNING_MESSAGE);
}
else {
String daiFuYuanChessBoard=chessboard.ForHUi();
String[] data=daiFuYuanChessBoard.split("\n");
chessboard.HuiQIGame(data);
repaint();
}
});
JButton review = new JButton("Review");
review.setLocation(HEIGTH, HEIGTH / 10 + 480);
review.setSize(200, 60);
review.setFont(new Font("Rockwell", Font.BOLD, 20));
add(review);
review.addActionListener(e->{
int length=chessboard.getIntStoreHuiQI();
int a=0;
TestThread testThread = new TestThread(chessboard);
testThread.start();
});
JButton chTH=new JButton("Change Theme");
chTH.setLocation(HEIGTH, HEIGTH / 10 + 560);
chTH.setSize(200, 60);
chTH.setFont(new Font("Rockwell", Font.BOLD, 20));
add(chTH);
chTH.addActionListener(e->{
Object[] options ={ "主题1", "主题2","默认"};
String s = (String) JOptionPane.showInputDialog(null,"请选择你的主题:\n", "切换", JOptionPane.PLAIN_MESSAGE, new ImageIcon("xx.png"), options, "xx");
if(s=="主题1"){
String path="./images/back2.png";
addChangeTheme(path);
chessboard.change("./images/slave.png","./images/smallYe.png");
}
else if(s=="主题2"){
String path="./images/back5.png";
addChangeTheme(path);
chessboard.change("./images/blacksmall.png","./images/smallYe.png");
}
else if (s=="默认"){
String path="./images/21.png";
addChangeTheme(path);
chessboard.change("./images/white11.png","./images/blacksmall.png");
}
});
}
// private static String savefile = "E:\\test.txt";
// private static void saveAsFileWriter(String content) {
//
// FileWriter fwriter = null;
// try {
// fwriter = new FileWriter(savefile);
// fwriter.write(content);
// } catch (IOException ex) {
// ex.printStackTrace();
// } finally {
// try {
// fwriter.flush();
// fwriter.close();
// } catch (IOException ex) {
// ex.printStackTrace();
// }
// }
// }
public void saveFile(String data) {//csdn
//弹出文件选择框
JFileChooser chooser = new JFileChooser();
//后缀名过滤器
// FileNameExtensionFilter filter = new FileNameExtensionFilter(
// "(*.txt)", "txt");
// chooser.setFileFilter(filter);
//下面的方法将阻塞,直到【用户按下保存按钮且“文件名”文本框不为空】或【用户按下取消按钮】
int option = chooser.showSaveDialog(null);
if(option==JFileChooser.APPROVE_OPTION){ //假如用户选择了保存
File file = chooser.getSelectedFile();
String fileName = chooser.getName(file); //从文件名输入框中获取文件名
//假如用户填写的文件名不带我们制定的后缀名,那么我们给它添上后缀
if(fileName.indexOf(".txt")==-1){
file = new File(chooser.getCurrentDirectory(),fileName+".txt");
System.out.println("renamed");
System.out.println(file.getName());
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data.getBytes());
fos.close();
} catch (IOException e) {
// System.err.println("IO异常");
e.printStackTrace();
}
}
}
// static void playMusic(){//背景音乐播放
// try {
// URL cb;
// File f = new File("F:\\QQmusicDownLoad\\music.wav"); // 引号里面的是音乐文件所在的路径
// cb = f.toURL();
// AudioClip aau;
// aau = Applet.newAudioClip(cb);
//
// aau.play();
// aau.loop();//循环播放
// System.out.println("可以播放");
// // 循环播放 aau.play()
// //单曲 aau.stop()停止播放
//
// } catch (MalformedURLException e) {
//
// e.printStackTrace();
// }
// }
public void addChangeBackground(){
JButton changeBackground = new JButton("change background");
changeBackground.setLocation(HEIGTH, HEIGTH / 10 + 320 );
changeBackground.setSize(200, 60);
add(changeBackground);
changeBackground.addActionListener(e -> {JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false); // 设为多选
int returnVal = chooser.showOpenDialog(changeBackground); // 是否打开文件选择框,选择保存是0,退出和1
if (returnVal == JFileChooser.APPROVE_OPTION) { //点击确定
remove(jb);
repaint();
String filepath = chooser.getSelectedFile().getAbsolutePath();
// System.out.println(filepath);
// System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
String path =filepath;
// System.out.println(path);
ImageIcon imag=new ImageIcon(path);
Image image=imag.getImage();
Image doSomImage=image.getScaledInstance(WIDTH,HEIGTH,Image.SCALE_FAST);
ImageIcon tureBackgroundd = new ImageIcon(doSomImage);
jb=new JLabel(tureBackgroundd);
jb.setBounds(0,0,getWidth(),getHeight());
add(jb);
jb.repaint();
}});
}
public void addChangeTheme(String path){
//点击确定
remove(jb);
repaint();
// System.out.println(filepath);
// System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
// System.out.println(path);
ImageIcon imag=new ImageIcon(path);
Image image=imag.getImage();
Image doSomImage=image.getScaledInstance(WIDTH,HEIGTH,Image.SCALE_FAST);
ImageIcon tureBackgroundd = new ImageIcon(doSomImage);
jb=new JLabel(tureBackgroundd);
jb.setBounds(0,0,getWidth(),getHeight());
add(jb);
jb.repaint();
}
}