/**
*
* @author 陆垚
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.awt.Font;
public class Main{
public static void main(String args[]){
BlackAndWhiteDesign b = new BlackAndWhiteDesign();
}
}
class BlackAndWhiteDesign extends Frame implements Runnable{
Go Background = new Go();
boolean stepColor = true;
int PORT;
Socket sendSocket;//主动连接Socket
PrintWriter writer;//用来发送message
boolean stopFlag;
boolean isInitiative;
Point messagePoint;
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
CheckboxGroup checkboxGroup1 = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox();
Checkbox checkbox2 = new Checkbox();
Label label1 = new Label();
TextField textField1 = new TextField(10);
Panel PtextField1 = new Panel();
Button button1 = new Button();
Label label2 = new Label();
Choice choice1 = new Choice();
Button button2 = new Button();
Button button3 = new Button();
BorderLayout borderLayout3 = new BorderLayout();
Panel panel4 = new Panel();
Panel panel4_1 = new Panel();
Panel panel4_2 = new Panel();
Panel panel4_3 = new Panel();
Label label3 = new Label("黑棋");
Label label4 = new Label("白棋");
JTextArea BlackNumber = new JTextArea("02"); //定义并初始化黑棋计数板
JTextArea WhiteNumber = new JTextArea("02"); //定义并初始化白棋计数板
BlackAndWhiteDesign(){
try
{
bwInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void bwInit() throws Exception{
this.setTitle("网络黑白棋Beta1.0 作者:陆垚");
new Thread(this).start();//启动监听线程
this.PORT = 1976;
this.isInitiative = false;//是否主动连接
this.stopFlag = false;//是否继续监听的标志
this.messagePoint = new Point();
choice1.setBackground(new Color(236, 190, 98));
this.choice1.addItem("黑");
this.choice1.addItem("白");
this.setBackground(new Color(236, 190, 98));
this.setSize(580,565);
this.setResizable(false);
this.setVisible(true);
checkbox1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
checkbox1_mouseClicked(e);
}
});
panel1.setLayout(new BorderLayout());
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("单机");
checkbox2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
checkbox2_mouseClicked(e);
}
});
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("联机");
label1.setText("对方地址");
button1.setBackground(new Color(236, 190, 98));
button1.setLabel("连接");
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
label2.setText(" ");
button2.setBackground(new Color(236, 190, 98));
button2.setLabel("开始");
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button2_actionPerformed(e);
}
});
button3.setBackground(new Color(236, 190, 98));
button3.setLabel("重新开始");
this.button3.setEnabled(false);
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button3_actionPerformed(e);
}
});
Background.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
Background_mouseClicked(e);
}
});
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this_windowClosing(e);
}
});
panel3.setLayout(new GridLayout(9, 1));
panel3.setBackground(new Color(236, 190, 98));
panel3.add(checkbox1);
panel3.add(checkbox2);
panel3.add(label1);
PtextField1.add(textField1);
panel3.add(PtextField1);
panel3.add(choice1);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
panel3.add(label2);
panel4.setLayout(new GridLayout(1, 3));
label3.setFont(new Font("h",15,30));
panel4_1.add(label3);
BlackNumber.setFont(new Font("h",15,30));
BlackNumber.setEditable(false);
panel4_1.add(BlackNumber);
label4.setFont(new Font("h",15,30));
panel4_2.add(label4);
WhiteNumber.setFont(new Font("h",15,30));
WhiteNumber.setEditable(false);
panel4_2.add(WhiteNumber);
panel4.add(panel4_1);
panel4.add(panel4_2);
panel4.add(panel4_3);
this.panel1.add(this.Background,BorderLayout.CENTER);
this.panel1.add(panel3, BorderLayout.EAST);
this.panel1.add(panel4, BorderLayout.SOUTH);
this.add(panel1, BorderLayout.CENTER);
this.disableLink();//废掉控件
this.checkboxGroup1.setSelectedCheckbox(this.checkbox1);
this.centerWindow();
this.Background.setEnabled(false);
this.setVisible(true);
}
void checkbox1_mouseClicked(MouseEvent e)
{
this.button2.setEnabled(true);
this.disableLink();
}
//disable联机时用的控件
void disableLink()
{
this.textField1.setBackground(new Color(236, 190, 98));
this.textField1.setEnabled(false);
this.choice1.setEnabled(false);
this.button1.setEnabled(false);
}
void checkbox2_mouseClicked(MouseEvent e)
{
this.enableLink();
}
void enableLink()
{
this.textField1.setBackground(Color.white);
this.textField1.setEnabled(true);
this.choice1.setEnabled(true);
this.button1.setEnabled(true);
this.button2.setEnabled(false);
}
void button1_actionPerformed(ActionEvent e)
{
this.stopFlag = false;
this.goToLink(this.textField1.getText().trim(),this.PORT);
this.button2.setEnabled(true);
}
//开始
void button2_actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("开始"))
{
this.stopFlag = false;
this.disableLink();
this.button3.setEnabled(true);
this.checkbox1.setEnabled(false);
this.checkbox2.setEnabled(false);
this.button2.setLabel("退出");
if(this.isSingle())
this.Background.setEnabled(true);
else//联机版时
{
if(this.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);
}
}
//判断类型
boolean isSingle()
{
return this.checkbox1.getState();