import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MainWindow extends Frame implements Runnable,ActionListener,ItemListener{
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();
Qipan qipan=new Qipan();
Panel panel1=new Panel();
Panel panel2=new Panel();
Image myImage;
int PORT;
Socket sendSocket;
PrintWriter writer;
boolean stopFlag;
Point messagePoint;
Point goStartPoint=null;
Point yellowPoint=null;
boolean stepColor=true;
public MainWindow(){
//创建控制面板区
choice1.setBackground(new Color(236,190,98));
button1.setBackground(new Color(236,190,98));
button2.setBackground(new Color(236,190,98));
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("单机");
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("联机");
label1.setText("对方地址");
button1.setLabel("连接");
this.choice1.addItem("黑");
this.choice1.addItem("白");
button2.setLabel("开始");
label2.setText(" ");
panel2.setLayout(new GridLayout(8,1,100,10));
panel2.setBackground(new Color(236,190,98));
panel2.add(checkbox1,null);
panel2.add(checkbox2,null);
panel2.add(label1,null);
panel2.add(textField1,null);
panel2.add(button1,null);
panel2.add(choice1,null);
panel2.add(button2,null);
panel2.add(label2,null);
//创建棋盘区
this.qipan.setEnabled(false);//开始之前屏蔽掉盘面
panel1.setLayout(new BorderLayout());
this.panel1.add(this.qipan,BorderLayout.CENTER);
this.panel1.add(panel2,BorderLayout.EAST);
//创建游戏主窗口
this.setSize(490,450);
this.setTitle("网络围棋游戏 韩伟");
this.setLayout(new BorderLayout());
this.add(panel1,BorderLayout.CENTER);
this.setResizable(false);
checkbox1.addItemListener(this);
checkbox2.addItemListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
qipan.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
qipan_mClicked(e);
}
});
qipan.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e){
qipan_mMoved(e);
}
});
new Thread(this).start();//启动监听线程
this.PORT=2005;
this.stopFlag=false;
messagePoint=new Point();
goStartPoint=this.qipan.getLocation();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Frame frm=(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
});
this.disableLink();
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 actionPerformed(ActionEvent e){
if(e.getSource()==button1){
this.goToLink(textField1.getText().trim(),PORT);
}
else if(e.getSource()==button2){
this.startGame(e);
}
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==checkbox1){
this.disableLink();
}
else if(e.getItemSelectable()==checkbox2){
this.enableLink();
}
}
//监听线程
public void run(){
try{
ServerSocket serverSocket=new ServerSocket(PORT);
Socket receiveSocket=null;
receiveSocket=serverSocket.accept();
checkboxGroup1.setSelectedCheckbox(checkbox2);
button1.setEnabled(false);
choice1.setEnabled(true);
textField1.setEnabled(false);
checkbox1.setEnabled(false);
checkbox2.setEnabled(false);
writer=new PrintWriter(receiveSocket.getOutputStream(),true);
BufferedReader reader=new BufferedReader(new
InputStreamReader(receiveSocket.getInputStream()));
String message;
this.qipan.showError("接收连接成功");
while(!this.stopFlag){
message=reader.readLine();
this.doMessage(message);
}
reader.close();
receiveSocket.close();
serverSocket.close();
}catch(IOException ioe){this.qipan.showError("意外中断");}
}
//处理接收到的信息
void doMessage(String message){
if(message.startsWith("start"))//判断开始
{
this.qipan.showError("对方已开始");
if(message.equals("start_black"))
this.choice1.select("白");
else
this.choice1.select("黑");
//只要你是黑的,就先走
if(this.choice1.getSelectedItem().equals("黑"))
this.qipan.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.qipan.setEnabled(true);//解禁
this.qipan.doStep(this.messagePoint,color);
}
}
//为鼠标定位
void qipan_mMoved(MouseEvent e){
Point realPoint=e.getPoint();
Point mousePoint=qipan.getMousePoint(realPoint,goStartPoint);
this.removeLastPoint(yellowPoint,mousePoint);
if(this.isPlace(mousePoint))
this.showMousePoint(mousePoint);
}
//加黄点的范围
boolean isPlace(Point p){
if(p.x>19||p.x<1||p.y<1||p.y>19)
return false;
int color;
CrossPoint one;
one=(CrossPoint)(this.qipan.myHash.get(p));
color=one.color;
if(color!=0)
return false;
return true;
}
void qipan_mClicked(MouseEvent e){
if(this.isSingle()){
this.doSingle();
}
else
{
this.doMultiple();
}
}
//开始
void startGame(ActionEvent e){
if(e.getActionCommand().equals("开始")){
this.disableLink();
this.checkbox1.setEnabled(false);
this.checkbox2.setEnabled(false);
this.button2.setLabel("退出");
if(this.isSingle())
qipan.setEnabled(true);
else//联机版时
{
if(choice1.getSelectedItem().equals("黑")){
this.writer.println("start_black");
}
else
this.writer.println("start_white");
}
this.paintMyColor();//表明颜色
}
else if(e.getActionCommand().equals("退出")){
this.dispose();
System.exit(0);
}
}
//disable联机时用的控件
void disableLink(){
this.textField1.setBackground(Color.white);
this.textField1.setEnabled(false);
this.choice1.setEnabled(false);
this.button1.setEnabled(false);
}
//enable联机时的控件
void enableLink(){
this.textField1.setBackground(Color.white);
this.textField1.setEnabled(true);
this.choice1.setEnabled(true);
this.button1.setEnabled(true);
}
//判断类型
boolean isSingle(){
return this.checkbox1.getState();
}
//加小黄点
void showMousePoint(Point mousePoint){
Graphics g=this.qipan.getGraphics();
g.setColor(Color.yellow);
g.fillOval(mousePoint.x*20-6,mousePoint.y*20-6,
this.qipan.INTERVAL-8,this.qipan.INTERVAL-8);
this.yellowPoint.x=mousePoint.x;//定位黄点
this.yellowPoint.y=mousePoint.y;
Graphics myG=this.myImage.getGraphics();
this.createMyImage(myG,this.yellowPoint,0);
}
//消除前一个黄点
void removeLastPoint(Point thatPoint,Point thisPoint){
if(thatPoint.x!=thisPoint.x||thatPoint.y!=thisPoint.y){
Graphics g=this.qipan.getGraphics();
if(thatPoint!=null&&this.myImage!=null)
g.drawImage(this.myImage,thatPoint.x*20-8,
thatPoint.y*20-8,16,16,null);
}
}
//构成所需要的image
void createMyImage(Graphics g,Point thisPoint,int color){
int px=thisPoint.x;
int py=thisPoint.y;
Color myColor=this.qipan.getBackground();
if(px==1&&py==1&&color==0)//四个角
{
g.setColor(myColor);
g.fillRect(0,0,16,16);
g.setColor(Color.black);
g.drawLine(8,8,8,16);
g.drawLine(5,5,5,16);
g.drawLine(8,8,16,8);
g.drawLine(5,5,16,5);
}
else if(px==1&&py==19&&color==0){
g.setColor(myColor);
g.fillRect(0,0,16,16);
g.setColor(Color.black);
g.drawLine(8,8,8,0);
g.drawLine(8,8,16,8);
g.drawLine(5,11,16,11);
g.drawLine(5,11,5,0);
}
else if(px==19&&py==1&&color==0){
g.setColor(myColor);
g.fillRect(0,0,16,16);
g.setColor(Color.black);
g.drawLine(8,8,8,16);
g.drawLine(8,8,0,8);
g.drawLine(11,5,11,16);
g.drawLine(11,5,0,5);
}
else if(px==19&&py==19&&color==0){
g.setColor(myColor);
g.fillRec
评论1