package MyGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.StringTokenizer;
import java.io.*;
public class MyGameFrame extends Frame {
private MyGameFrame f;
private boolean isblack = true;
private int[][] game;
private ServerSocket ss;
private Socket cs;
private int port = 6140;
private String serverip;
private DataOutputStream dout;
private DataInputStream din;
private Thread st;
private Thread ct;
private boolean flag = true;
private boolean isable;
// private String serverip;
private void initFrame() {
f = this;
this.setBackground(Color.YELLOW);
this.setSize(680, 680);
MenuBar mb = new MenuBar();
Menu mnsystem = new Menu("系统");
MenuItem mireplay = new MenuItem("重新开始");
mireplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*
* for (int i = f.getComponentCount() - 1; i >= 0; i--) {
* f.remove(i); }
*/
/*
* for(int i = 0;i < f.getHeight() / 40;i++) { for(int j = 0;i <
* f.getWidth() / 40;j++) { game[i][j] = 0; } }
*/
game = new int[f.getHeight() / 40][f.getWidth() / 40];
f.removeAll();
isblack = true;
}
});
MenuItem miundo = new MenuItem("悔棋一步");
miundo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Point point = f.getComponent(f.getComponentCount() - 1)
.getLocation();
int x1 = (int) (point.getX() + 20) / 40;
int y1 = (int) (point.getY() + 20) / 40;
game[y1][x1] = 0;
f.remove(f.getComponentCount() - 1);
isblack = (!isblack);
}
});
MenuItem mibuildserver = new MenuItem("建立主机");
mibuildserver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
st = new Thread(new Runnable() {
public void run() {
String packet = "";
int x1 = 0;
int y1 = 0;
try {
ss = new ServerSocket(port);
cs = ss.accept();
JOptionPane.showMessageDialog(f, "已经有玩家连接上了!",
"提示", JOptionPane.INFORMATION_MESSAGE);
dout = new DataOutputStream(cs.getOutputStream());
din = new DataInputStream(cs.getInputStream());
isable = true;
while (flag) {
packet = din.readUTF();
StringTokenizer getxy = new StringTokenizer(
packet, ",");
x1 = Integer.parseInt(getxy.nextToken());
int x = x1 * 40 - 20;
y1 = Integer.parseInt(getxy.nextToken());
int y = y1 * 40 - 20;
if (isblack) {
Black_Chess bc = new Black_Chess();
bc.setLocation(x, y);
f.add(bc);
game[y1][x1] = 1;
} else {
White_Chess wc = new White_Chess();
wc.setLocation(x, y);
f.add(wc);
game[y1][x1] = -1;
}
isblack = (!isblack);
isable = (!isable);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
st.start();
}
});
MenuItem miconnect = new MenuItem("连接主机");
miconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ct = new Thread(new Runnable() {
public void run() {
serverip = JOptionPane.showInputDialog(f,
"请输入主机的IP地址:", "提示",
JOptionPane.INFORMATION_MESSAGE);
String packet = "";
int x1 = 0;
int y1 = 0;
try {
/*
* JOptionPane.showMessageDialog(f, serverip, "提示",
* JOptionPane.INFORMATION_MESSAGE);
*/
cs = new Socket(serverip, port);
JOptionPane.showMessageDialog(f,
"成功连接主机,您可以开始游戏了!", "提示",
JOptionPane.INFORMATION_MESSAGE);
dout = new DataOutputStream(cs.getOutputStream());
din = new DataInputStream(cs.getInputStream());
isable = false;
while (flag) {
packet = din.readUTF();
/*
* JOptionPane.showMessageDialog(f, packet,
* "提示", JOptionPane.INFORMATION_MESSAGE);
*/
StringTokenizer getxy = new StringTokenizer(
packet, ",");
x1 = Integer.parseInt(getxy.nextToken());
int x = x1 * 40 - 20;
y1 = Integer.parseInt(getxy.nextToken());
int y = y1 * 40 - 20;
if (isblack) {
Black_Chess bc = new Black_Chess();
bc.setLocation(x, y);
f.add(bc);
game[y1][x1] = 1;
} else {
White_Chess wc = new White_Chess();
wc.setLocation(x, y);
f.add(wc);
game[y1][x1] = -1;
}
isblack = (!isblack);
isable = (!isable);
}
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
ct.start();
}
});
mnsystem.add(mibuildserver);
mnsystem.add(miconnect);
//mnsystem.add(mireplay);
//mnsystem.add(miundo);
mb.add(mnsystem);
this.setMenuBar(mb);
this.setTitle("五子棋");
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX() / 40;
int x1 = x;
x *= 40;
x -= 20;
int y = e.getY() / 40;
int y1 = y;
y *= 40;
y -= 20;
if(!isable)
{
JOptionPane.showMessageDialog(f, "对不起,现在不是轮到您下!","提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
if (game[y1][x1] == 0) {
if (isblack) {
Black_Chess bc = new Black_Chess();
bc.setLocation(x, y);
f.add(bc);
game[y1][x1] = 1;
} else {
White_Chess wc = new White_Chess();
wc.setLocation(x, y);
f.add(wc);
game[y1][x1] = -1;
}
isblack = (!isblack);
isable = (!isable);
try {
dout.writeUTF(x1 + "," + y1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(f, "请在别处下子!", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
Dimension screensize = this.getToolkit().getScreenSize();
this.setLocation((int) (screensize.getWidth() - this.getWidth()) / 2,
(int) (screensize.getHeight() - this.getHeight()) / 2);
game = new int[this.getHeight() / 40][this.getWidth() / 40];
this.setVisible(true);
}
public MyGameFrame() {
initFrame();
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
for (int i = 0; i < this.getWidth() / 40; i++) {
g.drawLine(0, i * 40, this.getWidth(), i * 40);
g.drawLine(i * 40, 0, i * 40, this.getHeight());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
MyGame.rar (20个子文件)
MyGame
.project 382B
bin
MyGame
MyGameFrame$2.class 1KB
MyGameFrame$1.class 963B
MyGameFrame$3.class 1KB
White_Chess.class 850B
Black_Chess.class 850B
MyGameFrame$5.class 2KB
White_Chess$1.class 778B
MyGameFrame$3$1.class 3KB
MyGameFrame$4.class 1KB
MyGameApplication.class 488B
MyGameFrame$4$1.class 4KB
MyGameFrame.class 5KB
MyGameFrame$6.class 679B
Black_Chess$1.class 778B
src
MyGame
White_Chess.java 817B
Black_Chess.java 802B
MyGameApplication.java 143B
MyGameFrame.java 7KB
.classpath 232B
共 20 条
- 1
heqin19870815
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0