📄 netgameinitdialog.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/**
*
* this class creates a dialog, it sets up the game rule
*
*/
class NetGameInitDialog extends Dialog
{
boolean dialogResult = false;
JButton bn1 = new JButton("确定");
JButton bn2 = new JButton("取消");
JRadioButton rb1 = new JRadioButton("黑棋", true);
JRadioButton rb2 = new JRadioButton("白棋");
ButtonGroup g = new ButtonGroup();
//the chessman held by local player
public ChessMan chessMan;
//total time of this game in seconds
public int totalSeconds;
JTextField tf = new JTextField(7);
JTextField tf1 = new JTextField(7);
//nonlocal player's name
String p2Name;
NetGameInitDialog(JFrame parent, int w, int h)
{
//set up the appearance
super(parent, "设置规则", true);
setSize(w, h);
setLocation(400, 300);
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 1));
JPanel p0 = new JPanel();
p0.add(new JLabel("我的昵称"));
p0.add(tf1);
p.add(p0);
JPanel p1 = new JPanel();
p1.add(new JLabel("本方执"));
g.add(rb1);
g.add(rb2);
p1.add(rb1);
p1.add(rb2);
p.add(p1);
JPanel p2 = new JPanel();
tf.setText("600");
p2.add(new JLabel("下棋总时间"));
p2.add(tf);
p.add(p2);
add("North", p);
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
p3.add(bn1);
p3.add(bn2);
add("South", p3);
//add action listener
bn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dialogResult = true;
setVisible(false);
}
});
bn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dialogResult = false;
setVisible(false);
}
});
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);}
});
}
}
/**
*
* this class creates a dialog waiting for client to join game
*
*/
class WaitingDialog extends Dialog
{
Socket sock;
Socket chatSock;
NetGameInitDialog dialog;
boolean dialogResult = false;
//thread listens for the client
Thread thr;
/**
*
* @param parent the parent of this dialog
* @param w the width of this dialog
* @param h the height
* @param d the former dialog
*/
WaitingDialog(JFrame parent, int w, int h, NetGameInitDialog d)
{
super(parent, "设置规则", true);
this.dialog = d;
setSize(w, h);
setLocation(400, 300);
//create a new thread and wait for client
thr = new Thread(){
public void run()
{
try{
dialog.totalSeconds = Integer.parseInt(dialog.tf.getText());
//listening and wait for client to join
ServerSocket serverSocket = new ServerSocket(MainFrame.chessPort);
sock = serverSocket.accept();
serverSocket = new ServerSocket(MainFrame.chatPort);
chatSock = serverSocket.accept();
//send information about whether a player hold black or white chess
if(dialog.rb1.isSelected() == true)
{
dialog.chessMan = ChessMan.Black;
sock.getOutputStream().write(1);
}
else
{
dialog.chessMan = ChessMan.White;
sock.getOutputStream().write(0);
}
//sends total seconds to client
(new DataOutputStream(sock.getOutputStream())).writeInt(dialog.totalSeconds);
//sends player's name
sock.getOutputStream().write(dialog.tf1.getText().getBytes());
//receive information from client
byte[] name = new byte[100];
//receive client's name
sock.getInputStream().read(name);
dialog.p2Name = new String(name);
dialog.p2Name = dialog.p2Name.trim();
//System.out.println(dialog.p2Name);
dialogResult = true;
}
catch(IOException ex)
{
System.out.println("Connection Error!");
}
catch(NumberFormatException ex)
{
System.out.println("String Parse Error!");
}
finally{
setVisible(false);
}
}
};
thr.start();
//set up layout of this dialog
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
JLabel statusLabel = new JLabel("等待客户端加入……");
statusLabel.setIcon(new ImageIcon(getClass().getResource(
"o_loading.gif")));
JButton bn3 = new JButton("取消");
add(statusLabel);
add(bn3);
//add action listener for canceling
bn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
if(sock != null)
sock.close();
if(chatSock != null)
chatSock.close();
}
catch(IOException ex)
{
System.out.println("Connection Error!");
}
finally
{
dialogResult = false;
setVisible(false);
}
return;
}
});
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);}
});
}
}
/**
*
* this dialog creates the client dialog
*
*/
class NetGameClientDialog extends Dialog
{
boolean dialogResult = false;
JButton bn1 = new JButton("连接");
JButton bn2 = new JButton("取消");
public int totalSeconds;
//input the name
JTextField tf = new JTextField(10);
//input the IP address
JTextField tf1 = new JTextField(10);
//the chess held by client
ChessMan chessMan;
//nonlocal player's name
String p2Name;
//sock for chess and chat
Socket sock;
Socket chatSock;
NetGameClientDialog(JFrame parent, int w, int h)
{
super(parent, "联机对战", true);
//set up appearance
setSize(w, h);
setLocation(400, 300);
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
JPanel p0 = new JPanel();
p0.add(new JLabel("我的昵称"));
p0.add(tf);
p.add(p0);
JPanel p1 = new JPanel();
p1.add(new JLabel("对方IP地址"));
p1.add(tf1);
p.add(p1);
add("North", p);
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
p3.add(bn1);
p3.add(bn2);
add("South", p3);
//add action listener
bn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
InetAddress addr = InetAddress.getByName(tf1.getText());
//connect to the server
sock = new Socket(addr, MainFrame.chessPort);
chatSock = new Socket(addr, MainFrame.chatPort);
//read the chess hold by client
if(sock.getInputStream().read() == 1)
chessMan = ChessMan.White;
else
chessMan = ChessMan.Black;
//read total time for the game
totalSeconds = (new DataInputStream(sock.getInputStream())).readInt();
//read nonlocal player's name
byte[] name = new byte[100];
sock.getInputStream().read(name);
p2Name = new String(name);
p2Name = p2Name.trim();
//send local player's name to the server
sock.getOutputStream().write(tf.getText().getBytes());
dialogResult = true;
}
catch(UnknownHostException ex)
{
System.out.println("Connection Error!");
}
catch(IOException ex)
{
System.out.println("Connection Error!");
}
finally{
setVisible(false);
}
}
});
bn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dialogResult = false;
setVisible(false);
}
});
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -