import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class MazeClient extends Applet implements Runnable{
public static final int BG_WIDTH=900;
public static final int BG_HEIGHT=580;
public MazeCanvas mazeCanvas=new MazeCanvas();
public TimerPlus timer=new TimerPlus();
int PORT;
Socket sendSocket;//主动连接Socket
PrintWriter writer;//用来发送message
boolean stopFlag;
boolean isInitiative;
Point messagePoint;
public TextField Northmessage;
public TextField SouthAbout;
TextField textEast = new TextField(10),
Northtext1 = new TextField(),
Northtext2 = new TextField();
Panel pNorth = new Panel(),
pWest = new Panel(),
pCenter = new Panel(),
pSouth = new Panel(),
pEast = new Panel();
CheckboxGroup checkboxGroup1 = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox();
Checkbox checkbox2 = new Checkbox();
JLabel bSouth=new JLabel("显示错误信息"),
bNorth=new JLabel("显示状态信息"),
bWest=new JLabel("我的时间,我做主哦!"),
bCenter=new JLabel("中心,游戏主窗口");
Label bEast1=new Label(),
bEast2=new Label();
Button button1 = new Button(),
button2 = new Button(),
button4 = new Button(),
button3 = new Button();
Choice choice1 = new Choice();
public void init() {
choice1.setBackground(new Color(236, 190, 98));
button1.setBackground(new Color(236, 190, 98));
this.resize(BG_WIDTH,BG_HEIGHT);
this.setVisible(true);
this.setLayout(new BorderLayout(10,7)); //
pCenter.setLayout(new BorderLayout(5,5)); //
pCenter.setSize(600,500);
new Thread(this).start();//启动监听线程
this.PORT=1976;
this.isInitiative=false;//是否主动连接
this.stopFlag=false;//是否继续监听的标志
messagePoint=new Point();
this.choice1.addItem("红");
this.choice1.addItem("蓝");
this.timer.setEnabled(false);//开始之前屏蔽掉计时器
this.mazeCanvas.setEnabled(false);//开始之前屏蔽掉游戏窗
checkbox1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
checkbox1_mouseClicked(e);
}
});
checkbox1.setCheckboxGroup(checkboxGroup1);
checkbox1.setLabel("单机");
checkbox2.setCheckboxGroup(checkboxGroup1);
checkbox2.setLabel("联机");
checkbox2.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
checkbox2_mouseClicked(e);
}
});
bEast1.setText("对方地址");
button1.setLabel("连接");
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
textEast.setEnabled(true);
bEast2.setText(" ");
button2.setBackground(new Color(236, 190, 98));
button2.setLabel("开始");
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button2_actionPerformed(e);
}
});
button4.setBackground(new Color(236, 190, 98));
button4.setLabel("REPLAY");
button4.setEnabled(false);
GridLayout gridLayout1=new GridLayout(9,1);
pEast.setLayout(gridLayout1);
gridLayout1.setVgap(10); //垂直间隙
pEast.setBackground(new Color(236, 190, 98));
pEast.add(checkbox1, null);
pEast.add(checkbox2, null);
pEast.add(bEast1, null);
pEast.add(textEast, null);
pEast.add(button1, null);
pEast.add(choice1, null);
pEast.add(button2, null);
pEast.add(bEast2, null);
pEast.add(button4, null);
this.add(pEast, BorderLayout.EAST); //东
this.bCenter.setBackground(new Color(236, 190, 98));
pCenter.add(bCenter,BorderLayout.NORTH); //中的北边
try {
this.mazeCanvas.jbInit(); //设置游戏窗颜色
} catch (Exception e1) {
e1.printStackTrace();
}
pCenter.add(this.mazeCanvas,BorderLayout.CENTER);
try {
this.timer.timerColor(); //设置游戏窗颜色
} catch (Exception e1) {
e1.printStackTrace();
}
pCenter.add(this.timer,BorderLayout.SOUTH); //中的南边
this.add(pCenter, BorderLayout.CENTER); //中
Northmessage=new TextField(57);
Northmessage.setEditable(false);
this.Northmessage.setBackground(new Color(236, 190, 98));
this.Northtext1.setBackground(new Color(236, 190, 98));
this.Northtext2.setBackground(new Color(236, 190, 98));
if (!this.checkbox1.getState()){ //????????????
Northmessage.setText("Use the arrow keys to solve."+"联机");
try {
Northtext2.setText("对方域名与IP:"+(InetAddress.getByName(this.textEast.getText().trim())).toString()
+this.textEast.getText().trim());//获取对方域名和IP地址
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
}
else{
Northmessage.setText("Use the arrow keys to solve. "+"单机");
}
try {
Northtext1.setText("本机域名与IP:"+(InetAddress.getLocalHost()).toString());
//获取本机域名和IP地址
} catch (UnknownHostException e) {
e.printStackTrace();
}
GridLayout gridLayout2=new GridLayout(1,4);
pNorth.setLayout(gridLayout2);
gridLayout2.setHgap(3);
gridLayout2.setVgap(10);
pNorth.setBackground(new Color(236, 190, 98));
pNorth.add(bNorth,null);
pNorth.add(Northmessage,null);
pNorth.add(Northtext1,null);
pNorth.add(Northtext2,null);
this.add(pNorth,BorderLayout.NORTH); //北
pSouth.setBackground(new Color(236, 190, 98));
SouthAbout=new TextField(50);
SouthAbout.setEditable(false);
this.SouthAbout.setBackground(new Color(236, 190, 98));
button3.setBackground(new Color(236, 190, 98));
button3.setLabel("About");
GridLayout gridLayout3=new GridLayout(1,3);
pSouth.setLayout(gridLayout3);
gridLayout3.setHgap(3);
gridLayout3.setVgap(10);
pSouth.add(bSouth,BorderLayout.WEST); //南的西边
pSouth.add(button3,null);
pSouth.add(SouthAbout,null);
this.add(pSouth,BorderLayout.SOUTH); //南
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button3_actionPerformed(e);
}
});
pWest.setLayout(new BorderLayout());
bWest.setBackground(new Color(236, 190, 98));
pWest.add(bWest,BorderLayout.SOUTH); //西的北边
try {
this.timer.timerColor(); //设置游戏窗颜色
} catch (Exception e1) {
e1.printStackTrace();
}
pWest.add(this.timer,BorderLayout.CENTER); //西的中
this.add(pWest,BorderLayout.WEST); //西
this.validate();
this.checkboxGroup1.setSelectedCheckbox(checkbox1); //默认单机
this.disableLink();//废掉控件
this.centerWindow();
}
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 void run() {
try{
ServerSocket serverSocket=new ServerSocket(PORT);
Socket receiveSocket=null;
receiveSocket=serverSocket.accept();
if(this.isInitiati