package cn.com.tqan;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import cn.com.tqan.ctrl.JMenuListener;
import cn.com.tqan.ctrl.PieceListener;
import cn.com.tqan.piece.impl.Gun;
import cn.com.tqan.piece.impl.Horse;
import cn.com.tqan.piece.impl.Ju;
import cn.com.tqan.piece.impl.Leader;
import cn.com.tqan.piece.impl.Official;
import cn.com.tqan.piece.impl.Prime;
import cn.com.tqan.piece.impl.Soldier;
import cn.com.tqan.pieceset.PieceSet;
public class Chessboard extends JFrame {
/**
* 主面板
*/
private JPanel mainPanel = null;
/**
* 上面的Panel
*/
private JPanel upPanel = null;
/**
* 这是摆放棋子每一个点
*/
private JLabel[][] pieceLabel = new JLabel[10][9];
/**
*
*/
private JMenuItem jitem = new JMenuItem("连接到主机");
/**
* 这是一个菜单项
*/
private JMenu jmenu = new JMenu("连接");
/**
* 这是一个菜单栏
*/
private JMenuBar jmb = new JMenuBar();
/**
* 这是棋盘上的棋子集合
*/
private PieceSet pieceSet = new PieceSet();
/**
* 这个表示你能不能走这一步了,也就是说,现在是应该你走是对方走
* 如果是true 则是你走的,false 是对方走的
*/
private boolean moveFlag = false;
/**
* 网络间进行传送的数据流
*/
private ObjectOutputStream oos = null;
/**
* 网络间进行接收数据 的数据流
*/
private ObjectInputStream ois = null;
/**
* 这是服务端的套接字
*/
private ServerSocket server ;
/**
* 这是连接的套接字
*/
private Socket s = null;
/**
* 点击事件监听器
*/
private PieceListener pl = new PieceListener(this);
/**
* 构造方法
*/
public Chessboard(){
super("中国象棋");
init();
// this.openServer(); //先开启服务端
// initPiece();
new Server(this).start();
}
/**
* 初使化方法
*/
private void init(){
// this.setLayout(new BorderLayout());
JLayeredPane jlp = this.getLayeredPane();
// this.setSize(600, 745);
this.setSize(600, 770);
initUpPanel();
initMainPanel();
mainPanel.setOpaque(false);
upPanel.setOpaque(false);
// mainPanel.setBounds(0, 0, 600, 740);
mainPanel.setBounds(0, 25, 600, 740);
jlp.add( mainPanel , 100 );
// upPanel.setBounds(15, 10, 580, 690);
upPanel.setBounds(15, 35, 580, 690);
jlp.add( upPanel, JLayeredPane.POPUP_LAYER );
mainPanel.setVisible(true);
upPanel.setVisible(true);
this.setResizable(false);
// this.add(mainPanel, BorderLayout.CENTER);
//==================++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
jmenu.add(jitem);
jmb.add(jmenu);
this.add(jmb, BorderLayout.NORTH);
jitem.addActionListener( new JMenuListener(this) );
//==================++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// mainPanel.repaint();
}
/**
* 上面的JPanel 的初使化
*
* 可以算出每一个格子的大小为64.444 X 69
*/
private void initUpPanel(){
upPanel = new JPanel( new GridLayout( 10 , 9 ) );
initPiece(); //初使化每个棋子
for( int i = 0 ; i < 10 ; i ++ ){
for( int j = 0 ; j < 9 ; j++ ){
upPanel.add(pieceLabel[i][j]); //把每一个棋子添加面板
}
}
addListener();//对每个棋子添加监听器
}
/**
* 对每个棋子添加监听器
*/
private void addListener(){
for( int i = 0 ; i < 10 ; i ++ ){
for( int j = 0 ; j < 9 ; j++ ){
pieceLabel[i][j].addMouseListener(pl);
}
}
}
/**
* 主面板初使化方法
*/
private void initMainPanel(){
mainPanel = new JPanel(){
public void paint(Graphics g) {
super.paint(g);
ImageIcon image = new ImageIcon( "images/qipan.jpg" );
// ImageIcon image = new ImageIcon( "D:\\workspace\\homework\\test8\\imags\\qipan.jpg" );
Image img = image.getImage();
g.drawImage(img, 0, 0, null);
}
};
}
/**
* 初使化棋子
* 也被使化了棋盘上的棋子集合
*/
private void initPiece(){
for( int i = 0 ; i < 10 ; i ++ ){
for( int j = 0 ; j < 9 ; j++ ){
pieceLabel[i][j] = new JLabel(new ImageIcon("images/piece/null.png"));
pieceLabel[i][j].setVisible(true);
}
}
//-----北边的兵
// Soldier soldier1 = new Soldier( new Point(0 , 3) ,"north" , pieceLabel[3][0] , new ImageIcon("images/piece/bing.png") , this.pieceSet );
Soldier soldier1 = new Soldier( new Point(0 , 3) ,"north" , pieceLabel[3][0] , "images/piece/bing.png" , this.pieceSet );
soldier1.setAvailable(true);
pieceSet.addPiece(soldier1);
// Soldier soldier2 = new Soldier( new Point(2 , 3) ,"north" , pieceLabel[3][2] , new ImageIcon("images/piece/bing.png") , this.pieceSet );
Soldier soldier2 = new Soldier( new Point(2 , 3) ,"north" , pieceLabel[3][2] , "images/piece/bing.png" , this.pieceSet );
soldier2.setAvailable(true);
pieceSet.addPiece(soldier2);
// Soldier soldier3 = new Soldier( new Point(4 , 3) ,"north" , pieceLabel[3][4] , new ImageIcon("images/piece/bing.png") , this.pieceSet );
Soldier soldier3 = new Soldier( new Point(4 , 3) ,"north" , pieceLabel[3][4] , "images/piece/bing.png" , this.pieceSet );
soldier3.setAvailable(true);
pieceSet.addPiece(soldier3);
// Soldier soldier4 = new Soldier( new Point(6 , 3) ,"north" , pieceLabel[3][6] , new ImageIcon("images/piece/bing.png") , this.pieceSet );
Soldier soldier4 = new Soldier( new Point(6 , 3) ,"north" , pieceLabel[3][6] , "images/piece/bing.png" , this.pieceSet );
soldier4.setAvailable(true);
pieceSet.addPiece(soldier4);
// Soldier soldier5 = new Soldier( new Point(8 , 3) ,"north" , pieceLabel[3][8] , new ImageIcon("images/piece/bing.png") , this.pieceSet );
Soldier soldier5 = new Soldier( new Point(8 , 3) ,"north" , pieceLabel[3][8] , "images/piece/bing.png" , this.pieceSet );
soldier5.setAvailable(true);
pieceSet.addPiece(soldier5);
//北边的炮
// Gun gunNorth1 = new Gun( new Point(1 , 2) ,"north" , pieceLabel[2][1] , new ImageIcon("images/piece/pao.png") , this.pieceSet );
Gun gunNorth1 = new Gun( new Point(1 , 2) ,"north" , pieceLabel[2][1] , "images/piece/pao.png" , this.pieceSet );
gunNorth1.setAvailable(true);
this.pieceSet.addPiece(gunNorth1);
// Gun gunNorth2 = new Gun( new Point(7 , 2) ,"north" , pieceLabel[2][7] , new ImageIcon("images/piece/pao.png") , this.pieceSet );
Gun gunNorth2 = new Gun( new Point(7 , 2) ,"north" , pieceLabel[2][7] , "images/piece/pao.png" , this.pieceSet );
gunNorth2.setAvailable(true);
this.pieceSet.addPiece(gunNorth2);
//北边最底下的一部份
//这是车
// Ju juNorthL = new Ju(new Point(0 , 0) ,"north" , pieceLabel[0][0] , new ImageIcon("images/piece/ju.png") , this.pieceSet);
Ju juNorthL = new Ju(new Point(0 , 0) ,"north" , pieceLabel[0][0] , "images/piece/ju.png" , this.pieceSet);
juNorthL.setAvailable(true);
this.pieceSet.addPiece(juNorthL);
// Ju juNorthR = new Ju(new Point(8 , 0) ,"north" , pieceLabel[0][8] , new ImageIcon("images/piece/ju.png") , this.pieceSet);
Ju juNorthR = new Ju(new Point(8 , 0) ,"north" , pieceLabel[0][8] , "images/piece/ju.png" , this.pieceSet);
juNorthR.setAvailable(true);
this.pieceSet.addPiece(juNorthR);
//这是马
// Horse horseNorthL = new Horse(new Point(1 , 0) ,"north" , pieceLabel[0][1] , new ImageIcon("images/piece/ma.png") , this.pieceSet);
Horse horseNorthL = new Horse(new Point(1 , 0) ,"north" , pieceLabel[0][1] , "images/piece/ma.png" , this.pieceSet);
horseNorthL.set