?? chesswzq.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: e-top</p>
* @author cylix
* @version 1.0
*/
public class ChessWZQ extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
protected Image white = null;
protected Image black = null;
protected static int pColor; // people 's color
protected static int cColor; // computer's color
static boolean ptocFlag=false;
boolean pFirst = false;
private int bestX=0; // computer find the best x coordinate to put chess
private int bestY=0; // computer find the best y coordinate to put chess
private int RectX1=0; // rectangle bounds according to (x,y)
private int RectY1=0; // length is 9
private int RectX2=0; // size 9*9
private int RectY2=0; // used to weight to each black position
private int weightBoard[][];
static Socket socket;
private static int PORT;
static ObjectInputStream in;
static ObjectOutputStream out;
String name=null ;
String serverAddress=null;
static char send[];
public static Message message = new Message();
public static boolean beginFlag = false;
BoardPanel bpanel = new BoardPanel();
JPanel jpanel3 = new JPanel();
JLabel label1 = new JLabel("Player1");
JLabel label2 = new JLabel(" VS ");
JLabel label3 = new JLabel("Player2");
JLabel label4 = new JLabel("Player List ");
JLabel label5 = new JLabel("Message list... ");
static JLabel label6 = new JLabel("welcome");
JLabel label7 = new JLabel("Host");
JLabel label8 = new JLabel("Player");
JRadioButton jrbBlack = new JRadioButton("Black");
JRadioButton jrbWhite = new JRadioButton("White");
DefaultListModel lItems = new DefaultListModel();
JList list = new JList(lItems);
JMenuBar mb = new JMenuBar();
JMenu create = new JMenu("Create");
JMenu setting = new JMenu("Setting");
JMenu quit = new JMenu("Quit");
JMenu about = new JMenu("About");
JMenuItem cPtoP = new JMenuItem("Play With people");
JMenuItem cPtoC = new JMenuItem("Play With Computer");
JMenuItem load = new JMenuItem("Load game...");
JMenuItem save = new JMenuItem("Save ...");
// JMenuItem sLimited = new JMenuItem("禁止禁手");
// JMenuItem sNoLimited = new JMenuItem("允許禁手");
public ChessWZQ() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
getContentPane().setLayout(null);
weightBoard = new int [15][15]; // save each position 's weight
PORT = Server.PORT;//set socket port
send = new char[60];
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
try {
getContentPane().setLayout(null);
jrbBlack.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
bpanel.setColor(1);
cColor=2;
jrbBlack.setEnabled(false);
jrbWhite.setEnabled(false);
drawChess(1);
jrbWhite.setSelected(true);
if(ptocFlag==true){
return;
}
Message ms = new Message();
ms.color=1;
ms.type=13;
try{
out.writeObject(ms);
}catch(IOException error){
error.printStackTrace();
}
}
});
jrbWhite.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
bpanel.setColor(2);
cColor=1;
jrbWhite.setEnabled(false);
jrbBlack.setEnabled(false);
drawChess(2);
jrbBlack.setSelected(false);
if(ptocFlag==true){
return;
}
Message ms = new Message();
ms.color=2;
ms.type=13;
try{
out.writeObject(ms);
}catch(IOException error){
error.printStackTrace();
}
}
});
about.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
JOptionPane.showMessageDialog(null,
"author: cylix \nCopyright (c) 2003 e-top\nmail:cylix_xtcc@sina.com",
"五子棋1.0(beta)",JOptionPane.INFORMATION_MESSAGE);
}
});
quit.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
int flag=JOptionPane.showConfirmDialog(null,
"Quit the Program ?",
"Are you sure to quit ?",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(flag==0){ //press YES
// disconnect to server
sendDisconnect();
System.exit(0);
}
}
});
setting.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Object selection[] = {"Forbiden","Allow any"};
Object set = JOptionPane.showInputDialog(null,
"Setting...","would you allow any method?",
JOptionPane.QUESTION_MESSAGE,null,selection,selection[0]);
if(ptocFlag==true){
return;
}
Message ms = new Message();
if(set==null){
return;
}
if(set.toString().equals(selection[0])){
ms.setting=false;
}
else{
ms.setting=true;
}
//System.out.println("setting begin..."+msg.setting);
ms.type=12;
try{
out.writeObject(ms);
}catch(IOException error){
error.printStackTrace();
}
}
});
cPtoP.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ptocFlag=false;
JOptionPane.showMessageDialog(null,
"You can choose a player from the listBox on the right","Welcome...",
JOptionPane.INFORMATION_MESSAGE);
}
});
cPtoC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(ptocFlag==true){
int flag=JOptionPane.showConfirmDialog(null,
"You give up... ?","Message",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(flag==0){
newGame();
return;
}
}
label3.setText("Computer");
Object selection[] = {"Computer First","You First"};
Object set = JOptionPane.showInputDialog(null,
"choose who first...","setting...",
JOptionPane.QUESTION_MESSAGE,null,
selection,selection[0]);
ptocFlag = true;
if(set.toString().equals(selection[1])){
pFirst = true;
}else{
pFirst = false;
}
pColor=bpanel.getColor();
if(pColor==1){
jrbBlack.setSelected(true);
jrbWhite.setSelected(false);
cColor=2;
}
else{
jrbWhite.setSelected(true);
jrbBlack.setSelected(false);
cColor=1;
}
jrbBlack.setEnabled(false);
jrbWhite.setEnabled(false);
ptoComputer();
}
});
create.setBounds(5,5,40,20);
setting.setBounds(45,5,40,20);
quit.setBounds(85,5,40,20);
about.setBounds(125,5,40,20);
create.add(cPtoP);
create.add(cPtoC);
create.add(load);
create.add(save);
mb.add(create);
mb.add(setting);
mb.add(quit);
mb.add(about);
this.setJMenuBar(mb);
bpanel.setBounds(0,0,470,460);
bpanel.setBorder(BorderFactory.createMatteBorder(0,0,1,1,Color.ORANGE));
jpanel3.setLayout(null);
jpanel3.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.DARK_GRAY));
jpanel3.setBounds(470,0,180,460);
label7.setBounds(10,10,30,20);
label7.setBackground(Color.blue);
label7.setForeground(Color.yellow);
label8.setBounds(90,10,40,30);
label8.setBackground(Color.blue);
label8.setForeground(Color.yellow);
label1.setBounds(10,40,50,20);
label2.setForeground(Color.RED);
label2.setBounds(60,40,50,20);
label3.setBounds(90,40,70,20);
label4.setForeground(Color.BLUE);
label4.setBounds(10,78,70,20);
label5.setBackground(Color.magenta);
label5.setForeground(Color.RED);
label5.setBounds(15,395,160,20);
label6.setBounds(15,415,180,20);
//label6.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.yellow));
jrbBlack.setSelected(true);
jrbBlack.setBounds(10,380,80,15);
jrbWhite.setBounds(90,380,80,15);
list.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
list.setBounds(10,100,150,265);
list.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
// double click to choose a player
if(e.getClickCount()==2){
String str = list.getSelectedValue().toString();
if(str.equals(name)==true){
label6.setText("You can't choose yourself");
return;
}
label3.setText(str);
list.setEnabled(false);
Message ms = new Message();
ms.type=3; //request play with another
strToCharArray(str,ms.msg);
ms.color = bpanel.getColor();
//System.out.println("request for play with "+str);
try{
out.writeObject(ms);
}catch(IOException er){
er.printStackTrace();
}
}
}
});
jpanel3.add(label1);
jpanel3.add(label2);
jpanel3.add(label3);
jpanel3.add(label4);
jpanel3.add(label5);
jpanel3.add(label6);
jpanel3.add(label7);
// jpanel3.add(label8);
jpanel3.add(jrbBlack);
jpanel3.add(jrbWhite);
jpanel3.add(list);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -