import java.awt.*;
//import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MainWindow extends Frame implements Runnable
{
Go panelGo=new Go();
Image myImage;
int PORT;
Socket sendSocket;//主动连接Socket
PrintWriter writer;//用来发送message
boolean stopFlag;
boolean isInitiative;
Point messagePoint;
Point goStartPoint=null;
Point yellowPoint=null;
boolean stepColor=true;
Point LastPoint=null;//移除黄点时,判断位置变动
BorderLayout borderLayout1 = new BorderLayout();
Panel panel1 = new Panel();
Panel panel2 = new Panel();
BorderLayout borderLayout2 = new BorderLayout();
Panel panel3 = new Panel();
CheckboxGroup checkboxGroup1 = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox();
Checkbox checkbox2 = new Checkbox();
Label label1 = new Label();
TextField textField1 = new TextField();
Button button1 = new Button();
Label label2 = new Label();
Choice choice1 = new Choice();
Button button2 = new Button();
GridLayout gridLayout1 = new GridLayout();
BorderLayout borderLayout3 = new BorderLayout();
public MainWindow()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
choice1.setBackground(new Color(236, 190, 98));
button1.setBackground(new Color(236, 190, 98));
// try
// {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// }catch(Exception e){e.printStackTrace();}
this.setResizable(false);
new Thread(this).start();//启动监听线程
this.PORT=1976;
this.isInitiative=false;//是否主动连接
this.stopFlag=false;//是否继续监听的标志
this.choice1.addItem("黑");
this.choice1.addItem("白");
LastPoint=new Point();
messagePoint=new Point();
this.setSize(470,450);
this.setTitle("围棋大战 制造者:苗泽");
this.panelGo.setEnabled(false);//开始之前屏蔽掉盘面
checkbox1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
checkbox1_mouseClicked(e);
}
});
this.goStartPoint=this.panelGo.getLocation();//
this.setLayout(borderLayout1);
panel1.setLayout(borderLayout2);
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("单机");
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("联机");
checkbox2.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
checkbox2_mouseClicked(e);
}
});
label1.setText("对方地址");
button1.setLabel("连接");
button1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
label2.setText(" ");
button2.setBackground(new Color(236, 190, 98));
button2.setLabel("开始");
button2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button2_actionPerformed(e);
}
});
panel3.setLayout(gridLayout1);
gridLayout1.setRows(8);
gridLayout1.setColumns(1);
gridLayout1.setHgap(100);
gridLayout1.setVgap(10);
panel2.setLayout(borderLayout3);
this.panel2.setSize(500,70);
panelGo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
panelGo_mouseMoved(e);
}
});
panelGo.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
panelGo_mouseClicked(e);
}
});
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this_windowClosing(e);
}
});
panel3.setBackground(new Color(236, 190, 98));
panel3.add(checkbox1, null);
panel3.add(checkbox2, null);
panel3.add(label1, null);
panel3.add(textField1, null);
panel3.add(button1, null);
panel3.add(choice1, null);
panel3.add(button2, null);
panel3.add(label2, null);
this.panel1.add(this.panelGo,BorderLayout.CENTER);
this.panel1.add(panel3, BorderLayout.EAST);
this.add(panel2, BorderLayout.SOUTH);
this.add(panel1, BorderLayout.CENTER);
this.disableLink();//废掉控件
//this.button2.setEnabled(false);//废掉开始
this.checkboxGroup1.setSelectedCheckbox(this.checkbox1);
this.yellowPoint=new Point(1000,1000);//初始化一个世纪外的黄点
this.centerWindow();
this.show();
myImage=this.createImage(16,16);//用来纪录有黄点之前的图像
}
void centerWindow()
{
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
int pX=(d.width-this.getWidth())/2;
int pY=(d.height-this.getHeight())/2;
this.setLocation(pX,pY);
}
public static void main(String args[])
{
MainWindow main=new MainWindow();
}
//监听线程
public void run()
{
try
{
ServerSocket serverSocket=new ServerSocket(PORT);
Socket receiveSocket=null;
receiveSocket=serverSocket.accept();
if(this.isInitiative)//如果已在进行,则不接受连接
this.stopFlag=true;
this.checkboxGroup1.setSelectedCheckbox(this.checkbox2);//自动选择联机
this.button1.setEnabled(false);
this.choice1.setEnabled(true);
this.textField1.setEnabled(false);
this.checkbox1.setEnabled(false);
this.checkbox2.setEnabled(false);
this.writer=new PrintWriter(receiveSocket.getOutputStream(),true);
BufferedReader reader=new BufferedReader(new InputStreamReader(receiveSocket.getInputStream()));
String message;
while(!this.stopFlag)
{
this.panelGo.showError("接收连接成功");
message=reader.readLine();
this.doMessage(message);
}
reader.close();
receiveSocket.close();
serverSocket.close();
}catch(IOException ioe){this.panelGo.showError("您赢了");}
}
//处理接收到的东西
void doMessage(String message)
{
if(message.startsWith("start"))//判断开始
{
this.panelGo.showError("开始了");
if(message.equals("start_black"))
this.choice1.select("白");
else
this.choice1.select("黑");
if(this.choice1.getSelectedItem().equals("黑"))//只要你是黑的,就先走
this.panelGo.setEnabled(true);
this.paintMyColor();//表明颜色
this.disableLink();
}
else//下子的信息
{
int color=Integer.parseInt(message.substring(0,1));
this.messagePoint.x=Integer.parseInt(message.substring(1,3));
this.messagePoint.y=Integer.parseInt(message.substring(3,5));
this.panelGo.setEnabled(true);//解禁
this.panelGo.doStep(this.messagePoint,color);
}
}
//为鼠标定位
void panelGo_mouseMoved(MouseEvent e)
{
Point realPoint=e.getPoint();
Point mousePoint=this.panelGo.getMousePoint(realPoint,this.goStartPoint);
this.removeLastMousePoint(this.LastPoint,mousePoint);
//this.LastPoint=mousePoint;大错误,使对

豆包程序员
- 粉丝: 1w+
最新资源
- 2020年本科软件工程开题报告(1).doc
- 计算机实习日记范文汇编四篇(1).doc
- 智慧加油站物联网大数据集成项目建设方案.docx
- 关于网络安全保障助力广播电视信息化发展的探讨(1).docx
- 3G时代通信企业管理模式研究(1).docx
- 组态王大型污水处理厂SCADA系统解决方案(1).doc
- XXXX灌区信息化建设项目-设计方案(1).doc
- 编译原理语法制导翻译课件市公开课一等奖省赛课微课金奖课件(1).pptx
- 电子商务网站需求分析书(1).pdf
- 珠宝类网站平台项目方案(1)(1).docx
- 结构施工图的主要内容及计算机绘图技巧(1).doc
- 图书管理系统本科学位论文(1)(1).doc
- 推进教育信息化联盟策划研讨方案样本(1).doc
- 计算机个人实习总结(1).docx
- 网站建设合同(合集15篇)(1).doc
- 计算机专业实习报告集合7篇(1).doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0