-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.java
More file actions
123 lines (101 loc) · 3.28 KB
/
Copy pathUI.java
File metadata and controls
123 lines (101 loc) · 3.28 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
package ui;
import data.Config;
import data.Game;
import judge.Judge;
import modular.Indicator;
import objects.Chess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UI extends JPanel implements ActionListener {
JFrame frame;
public UI() {
initFrame();
initMenu();
Timer timer = new Timer(1 / Config.FPS, this);
timer.start();
frame.setVisible(true);
}
// 初始化各种参数
public synchronized void initFrame() {
frame = new JFrame("五子棋");
frame.setSize(780, 635);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.add(this, BorderLayout.CENTER);
}
public synchronized void initMenu() {
new Menu(frame, new Judge(frame));
}
// 绘制图形
@Override
public void paint(Graphics g) {
super.paintComponent(g);
drawBackground(g);
drawChessboard(g);
drawChess(g);
drawIndicator(g);
drawWeightChessGrade(g);
}
// 各种组件绘制方法
public void drawBackground(Graphics ignoredG) {
setBackground(new Color(209, 146, 17));
}
public void drawChessboard(Graphics g) {
for (int i = 0; i < 15; i++) {
g.drawLine(20, 20 + 40 * i, 20 + 40 * (15 - 1), 20 + 40 * i);// 横线 //格子40
g.drawLine(20 + 40 * i, 20, 20 + 40 * i, 20 + 40 * (15 - 1));// 竖线 //格子40
}
//五个黑点绘制
int x = 136;
int y = 136;
g.fillArc(x, y, 8, 8, 0, 360);
g.fillArc(x + 320, y + 320, 8, 8, 0, 360);
g.fillArc(x + 320, y, 8, 8, 0, 360);
g.fillArc(x, y + 320, 8, 8, 0, 360);
g.fillArc(x + 160, y + 160, 8, 8, 0, 360);
}
public void drawChess(Graphics g) {
for (Chess chess : Game.chessArray) {
g.setColor(chess.getColor());
int row = chess.row;
int column = chess.column;
g.fillOval(row * 40, column * 40, 40, 40);
}
}
public void drawIndicator(Graphics g) {
Indicator.drawIndicator(g);
}
public void drawWeightChessGrade(Graphics g) {
if (Config.ifShowWeightChess) {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
g.setColor(Color.green);
g.setFont(new Font("黑体", Font.BOLD, 15));
int number = Game.weightArray[j][i];
for (Chess chess : Game.chessArray) {
if (chess.equal(new Chess(j, i, Color.black))) {
number = 0;
}
}
if (number == 0) {
g.setColor(Color.red);
} else {
g.setColor(Color.green);
}
String grade = String.valueOf(number);
g.drawString(grade, j * 40 + 20, i * 40 + 20);
}
}
}
}
// 自动刷新棋盘
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}