import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.image.*;
public class Server extends JFrame implements ActionListener {
// move|1|1
JPanel contentPane;
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();//侦听按钮
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();//发送按钮
JButton jButton3 = new JButton();//悔棋按钮
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
ServerSocket server = null;
Socket socket = null;
BufferedReader instr = null;
PrintWriter os = null;
public static String[] ss = new String[10];
// 保存棋子的坐标
int x = 0;
int y = 0;
// 保存之前下过的全部棋子的坐标
// 其中数据内容 0: 表示这个点并没有棋子,1:表示这个点是黑子,2:表示是白子
int[][] allChess = new int[19][19];
boolean isBlack = true;// 自己是黑棋
// 标识当前游戏是否可以继续
boolean canPlay = true;
// 保存显示的提示信息
String message ="";// "自己是黑方先行";
JPanel panel1 = new JPanel();
GobangPanel panel2 = new GobangPanel();
/**
* 服务器端构造方法
*/
public Server() {
jbInit();
}
// Component initialization
private void jbInit() {
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(540, 640));
this.setTitle("服务器");
jLabel2.setBounds(new Rectangle(22, 0, 72, 28));
jLabel2.setText("端口号");
jLabel2.setFont(new java.awt.Font("宋体", 0, 14));
jTextField2.setBounds(new Rectangle(73, 0, 45, 24));
jButton1.setBounds(new Rectangle(120, 0, 73, 25));
jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setActionCommand("jButton1");
jButton1.setText("侦听");
jLabel3.setBounds(new Rectangle(200, 0, 87, 28));
jLabel3.setText("请输入信息");
jLabel3.setFont(new java.awt.Font("宋体", 0, 14));
jTextField3.setBounds(new Rectangle(274, 0, 154, 24));
jTextField3.setText("");
jButton2.setText("发送");
jButton2.setActionCommand("jButton1");
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
jButton2.setBounds(new Rectangle(430, 0, 43, 25));
jButton3.setText("悔棋");
jButton3.setActionCommand("jButton1");
jButton3.setBorder(BorderFactory.createEtchedBorder());
jButton3.setFont(new java.awt.Font("Dialog", 0, 14));
jButton3.setBounds(new Rectangle(480, 0, 43, 25));
jScrollPane1.setBounds(new Rectangle(23, 28, 493, 89));
// 7&&&&&
jTextField3.setText("此处输入发送信息");
jTextArea1.setText("聊天内容");
panel1.setLayout(null);// panel1.setLayout(flayout);
panel1.add(jLabel2);
panel1.add(jTextField2);
panel1.add(jButton1);
panel1.add(jLabel3);
panel1.add(jTextField3);
panel1.add(jButton2);
panel1.add(jButton3);
panel1.add(jScrollPane1);
jScrollPane1.getViewport().add(jTextArea1);
// xmj
contentPane.setLayout(null);
contentPane.add(panel1);
contentPane.add(panel2);
panel1.setBounds(0, 0, 540, 120);
panel2.setBounds(10, 120, 540, 460);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
sendData("quit|");//向对方发送离开信息
socket.close();
instr.close();
System.exit(0);
} catch (Exception ex) {
}
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton1) {// 侦听
int port = Integer.parseInt(jTextField2.getText().trim());
listenClient(port);
System.out.print("侦听...");
}
if (e.getSource() == jButton2) {// 发送文字
String s = this.jTextField3.getText().trim();
sendData(s);
System.out.print("发送文字");
}
if (e.getSource() == jButton3) {// 悔棋
if (canPlay != true) {// 该对方走棋
allChess[x][y] = 0;
panel2.repaint();
canPlay = true;
String s = "undo|" + x + "|" + y;
sendData(s);
System.out.print("发送悔棋信息");
} else// 对方已走棋
{
message = "对方已走棋,不能悔棋了";
JOptionPane.showMessageDialog(this, message);
System.out.print("对方已走棋,不能悔棋了");
}
}
}
private void listenClient(final int port) {// 侦听
try {
if (jButton1.getText().trim().equals("侦听")) {
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
server = new ServerSocket(port);
jButton1.setText("正在侦听...");
socket = server.accept();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 等待,一直到客户端连接才望下执行
// this.setTitle("你是黑方");
sendData("已经成功连接。。。");
jButton1.setText("正在聊天...");
jTextArea1.append("客户端已经连接到服务器\n");
message = "自己是黑方先行";
panel2.repaint();
MyThread t = new MyThread();
t.start();
}
}).start();
}
} catch (Exception ex) {
}
}
// 内部线程类
class MyThread extends Thread {// 该线程负责接受数据
public void run() {
try {
while (true) {
this.sleep(100);
instr = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
if (instr.ready()) { // 检查是否有数据
String cmd = instr.readLine();
jTextArea1.append("客户端: " + cmd + "\n");
// 在每个|字符处进行分解。
ss = cmd.split("\\|");
if (cmd.startsWith("move")) {
message = "轮到自己下棋子";
int x = Integer.parseInt(ss[1]);
int y = Integer.parseInt(ss[2]);
allChess[x][y] = 2;
panel2.repaint();
canPlay = true;
}
if (cmd.startsWith("undo")) {
JOptionPane.showMessageDialog(null, "对方撤销上步棋");
int x = Integer.parseInt(ss[1]);
int y = Integer.parseInt(ss[2]);
allChess[x][y] = 0;
panel2.repaint();
canPlay = false;
}
if (cmd.startsWith("over")) {
JOptionPane.showMessageDialog(null, message);
panel2.setEnabled(false);
canPlay = false;
}
if (cmd.startsWith("quit")) {
JOptionPane.showMessageDialog(null, "游戏结束,对方离开了!!");
panel2.setEnabled(false);
canPlay = false;
}
}
}
} catch (Exception ex) {
System.out.print("error: " + ex);
}
}
}
private void sendData(String s) {// 发送数据
try {
os = new PrintWriter(socket.getOutputStream());
os.println(s);
os.flush();
if (!s.equals("已经成功连接。。。"))
this.jTextArea1.append("Server:" + s + "\n");
} catch (Exception ex) {
}
}
public static void main(String arg[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
Server frm = new Server();
frm.setVisible(true);
try {
InetAddress address = InetAddress.getLocalHost().getLocalHost();
frm.setTitle(frm.getTitle()+"名称及IP地址:"+address.toString());
} catch (Exception e) {
// 异常处理代码
}
}
class GobangPanel extends JPanel {
BufferedImage bgImage = null;// 背景图片
GobangPanel() {
this.addMouseListener(new MouseLis());
String imagePath = "";
try {
imagePath = System.getProperty("user.dir") + "/background2.jpg";
System.out.print(imagePath);
bgImage = ImageIO.read(new File(imagePath.replaceAll("\\\\",
"/")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 双缓冲技术防止屏幕闪烁
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_RG
2020年最新版Java五子棋(可网络对战)
需积分: 50 132 浏览量
2020-01-03
15:51:15
上传
评论 10
收藏 48KB ZIP 举报
Geek_H
- 粉丝: 26
- 资源: 9
最新资源
- yolov5,SSD 可能使用到的一些代码
- 介绍离散性制造行业的MES系统流程
- 基于IDEA-CCNL/Randeng-Pegasus-238M-Summary-Chines微调的中文文本摘要任务源码+数据集
- 微信小程序源码 车源宝 二手车交易平台 源码下载
- 微信小程序源码 实现 城市切换 demo 根据城市首字母排序城市 选择城市 源码下载
- 2024新版计算机编译原理期末速成全套视频教程(视频+配套资料)
- VMware7.0虚拟机硬盘无法编辑,无法连接到Profile-Driven Storage Service
- arm64内核的mongo镜像
- 基于stm32f103c单片机+MPU6050+0.96英寸OLED显示屏双柄遥控器硬件(原理图+PCB)工程文件.zip
- 整理的关于少儿编程的学习路径,以及如何在小升初,初升高和大学充分的利用起来编程经验的优势
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈