本文实例为大家分享了java实现五子棋网络版的具体代码,供大家参考,具体内容如下
需求分析:
1.拥有服务器端和客户端,用户通过客户端登录服务器后可与其他登录的用户进行对弈
2.服务器支持多组用户同时进行对弈
3.用户可以在服务器上创建新游戏或加入已创建的游戏
4.用户在下棋的时候可以进行聊天交流
由上可以知道需要实现的功能:
·提供服务器和客户端的功能
·服务器将监听客户端的登录情况并允许多个客户端进行登录
·用户通过客户端可以登录服务器,之后可以看到服务器当前在线的其他用户,并与他们进行聊天等
·用户登录服务器后,可以创建新的五子棋游戏或加入已创建的五子棋游戏
·用户通过客户端可以像普通五子棋那样与其他用户对弈
根据功能将网络五子棋分为4个模块:即用户面板模块、棋盘面板模块、五子棋服务器模块、五子棋客户端模块
下面我们开始进行编译用户面板模块:
1.开发用户列表面板
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import java.awt.*;
/**
* created by administrator on 2016/11/21.
*/
//初始状态下将添加10个名称为“无用户“的信息到列表中,说明服务器最多支持10个用户同时在线
//该列表被添加到面板中,使用“borderlayout”布局格式
public class userlistpad extends panel{
public list userlist= new list( 10 );
public userlistpad(){
setlayout( new borderlayout());
for ( int i= 0 ;i< 10 ;i++){
userlist.add(i+ "." + "无用户" );
}
add(userlist,borderlayout.center);
}
}
|
2.开发用户聊天面板
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import javax.swing.*;
import java.awt.*;
/**
* created by administrator on 2016/11/21.
*/
//聊天面板为一个textarea视图控件,拥有一个垂直方向的滚动条。
//该textarea被添加到面板中,使用“borderlayout”布局格式。
public class userchatpad extends jpanel{
public jtextarea chattextarea= new jtextarea( "命令区域" , 18 , 20 );
public userchatpad(){
setlayout( new borderlayout());
chattextarea.setautoscrolls( true );
chattextarea.setlinewrap( true );
add(chattextarea,borderlayout.center);
}
}
|
3.开发用户输入面板
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import javax.swing.*;
import java.awt.*;
/**
* created by administrator on 2016/11/21.
*/
//面板包含两个视图控件
//contentinpitted为textfield控件,用户可以在其中输入聊天信息
public class userinputpad extends jpanel{
public jtextfield contentinputted = new jtextfield( "" , 26 );
public jcombobox userchoice = new jcombobox();
public userinputpad(){
setlayout( new flowlayout(flowlayout.left));
for ( int i= 0 ;i< 50 ;i++){
userchoice.additem(i+ "." + "无用户" );
}
userchoice.setsize( 60 , 24 );
add(userchoice);
add(contentinputted);
}
}
|
4.开发用户操作面板
?
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
|
import javax.swing.*;
import java.awt.*;
/**
* created by administrator on 2016/11/21.
*/
public class usercontrolpad extends jpanel {
public jlabel iplabel = new jlabel( "ip" ,jlabel.left);
public jtextfield ipinputted = new jtextfield( "localhost" , 10 );
public jbutton connectbutton = new jbutton( "连接到服务器" );
public jbutton createbutton = new jbutton( "创建游戏" );
public jbutton joinbutton = new jbutton( "加入游戏" );
public jbutton cancelbutton = new jbutton( "放弃游戏" );
public jbutton exitbutton = new jbutton( "退出游戏" );
public usercontrolpad(){
setlayout( new flowlayout(flowlayout.left));
setbackground(color.light_gray);
add(iplabel);
add(ipinputted);
add(connectbutton);
add(createbutton);
add(joinbutton);
add(cancelbutton);
add(exitbutton);
}
}
|
下面开始开发棋盘面板模块
1.开发黑棋类
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import java.awt.*;
/**
* created by administrator on 2016/11/21.
*/
public class firpointblack extends canvas {
firpad padbelonged; // 黑棋所属的棋盘
public firpointblack(firpad padbelonged)
{
setsize( 20 , 20 ); // 设置棋子大小
this .padbelonged = padbelonged;
}
public void paint(graphics g)
{ // 画棋子
g.setcolor(color.black);
g.filloval( 0 , 0 , 14 , 14 );
}
}
|
2.开发白棋类
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import java.awt.*;
/**
* created by administrator on 2016/11/21.
*/
public class firpointwhite extends canvas{
firpad padbelonged; // 白棋所属的棋盘
public firpointwhite(firpad padbelonged)
{
setsize( 20 , 20 );
this .padbelonged = padbelonged;
}
public void paint(graphics g)
{ // 画棋子
g.setcolor(color.white);
g.filloval( 0 , 0 , 14 , 14 );
}
}
|
3.开发棋盘面板
?
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
|