⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chessboard.java

📁 网络象棋对战程序,模拟vista的国际象棋功能,可以自动寻找路径,棋子随同鼠标移动.另有网络聊天功能. ps:里面用的登陆服务器地址,需要稍做修改,修改成你想登陆的服务器. 开发工具eclipse
💻 JAVA
字号:
/**
 * 
 */
package client;

/**
 * @author Justok
 *
 */
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;

public class ChessBoard extends JPanel 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8769151014257800651L;



	/**
	 * @param args
	 */
	public static final int a = 50;  //the length of every small rectangle
	public static int  offset = 50;  //use when drawBoard()
	public static int piclength = 41;
	public static Chessman [] chess;// = new Chessman[34];//1- 16 green 17 - 32 red
	public static int [][] moment;// = new int[9][10];
	public static java.util.Vector<step> movelog;// = new java.util.Vector<step>(60, 10);
	public static java.util.Vector<java.awt.Point> theap = new java.util.Vector<java.awt.Point>(10,2);
	
	public ChessBoard()
	{
		//System.out.println("ChessBoard()");
		MyMouseListener myml = new MyMouseListener();
		
		addMouseListener(myml);
		addMouseMotionListener(myml);
		
		initChessman();	
		
		paintComponents(this.getGraphics());
		//System.out.println("OK:ChessBoard()");
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		//System.out.println("\tpaintGraphics("+g+"):begin");
		drawBoardLines(g);
		
		putChess(g);
		
		drawAvailable(g,MyMouseListener.chesson);
		
		//System.out.println("\tpaintGraphics("+g+"):end");
	}//paintComponent
	
	
	public void drawAvailable(Graphics g, int id)
	{
		if(id == 0)
			return;
		AvailablePlace idap = new AvailablePlace(id, ChessBoard.moment);
		theap = (java.util.Vector<java.awt.Point>)idap.ap;
		java.util.Iterator<java.awt.Point> iter =  theap.iterator();//new java.util.Iterator<new java.awt.Point>();
		java.awt.Point idp;
		while(iter.hasNext())
		{
			idp = iter.next();
			(new drawCircle(g,idp.x,idp.y)).start();
		}
	}//drawAvailable()
	

	
	public static void initChessman()
	{
		chess = new Chessman[34];
		moment = new int[9][10];
		movelog = new java.util.Vector<step>(60,10);
		
		String [] addr = {"您执红棋.gif", ".\\pic\\blue5.png",".\\pic\\blue4.png",".\\pic\\blue3.png",".\\pic\\blue2.png", 
						// 0        	  1   che 车        	 	2 ma 马	          3	 xiang	         	4 shi        
		   ".\\pic\\blue1.png",".\\pic\\blue2.png",".\\pic\\blue3.png",".\\pic\\blue4.png",
		   //  5   shuai   	    6  shi          	   7	xiang	         8 ma              
		   ".\\pic\\blue5.png", ".\\pic\\blue6.png",".\\pic\\blue6.png", 
		   //9     che   	      10 pao	           11 pao
		   ".\\pic\\blue7.png",".\\pic\\blue7.png",".\\pic\\blue7.png",".\\pic\\blue7.png",".\\pic\\blue7.png",
      //     12     bing	   13     bing    	14  bing         15     bing    16  bing   
                       //1 - 16 是绿棋               
         ".\\pic\\red5.png",".\\pic\\red4.png", ".\\pic\\red3.png",".\\pic\\red2.png",
 //        		17 	che			18 ma				19	xiang		     20 shi			
         ".\\pic\\red1.png",".\\pic\\red2.png",".\\pic\\red3.png",".\\pic\\red4.png",
         //21	 jiang			22 shi	 	  	 	23 xiang	 		24	ma
         ".\\pic\\red5.png",".\\pic\\red6.png", ".\\pic\\red6.png", 
//      	25	che				26	pao			27  pao			
         ".\\pic\\red7.png",".\\pic\\red7.png",".\\pic\\red7.png",".\\pic\\red7.png", ".\\pic\\red7.png", "绿棋出招.gif"};
 //         28 bing				29 bing				30	bing	  31			32	       33 
           //  17 - 32 是红棋
	//1-16 g.\\pic\\reden chessmen and 17 to 32 .\\pic\\red chessman. 0 and 33 a.\\pic\\red functional pic
		int [] ix = {-1,0,1,2,3,4,5,6,7,8,1,7,0,2,4,6,8};//9,8,7-1}; 共1装图片加16子,标号0-16
		int [] iy = {-1,0,0,0,0,0,0,0,0,0,2,2,3,3,3,3,3};//,0,0,0,0,0,0,0,0,0,2,2,3,3,3,3,3,-1};
		//chess = new Chessman[34];
		String [] iname = {"无","绿车","绿马","绿象","绿士","绿将","绿士","绿象","绿马","绿车","绿炮","绿炮","绿卒","绿卒","绿卒","绿卒","绿卒",
								"红车","红马","红相","红士","红帅	","红士","红相","红马","红车","红炮","红炮","红兵","红兵","红兵","红兵","红兵","无"};
		for(int i = 0; i < 8; i++)
			for(int j = 0; j < 9; j++)
				moment[i][j] = 0;
		
		for(int i = 1; i < 17; i++)
		{
			chess[i] = new Chessman(addr[i],8-ix[i],9-iy[i],iname[i]);
			moment[8-ix[i]][9-iy[i]] = i;
			
			chess[i+16] = new Chessman(addr[i+16],ix[i],iy[i],iname[i+16]);
			moment[ix[i]][iy[i]] = i+16;
		}
		
		chess[0] = new Chessman(addr[0],ix[0],iy[0],iname[0]);
		chess[33] = new Chessman(addr[0],ix[0],iy[0],iname[0]);
		
		for(int i = 1; i < 33; i++)
		{
			chess[i].alive = true;
		}
	}//initChessman

	
	protected void drawBoardLines(Graphics g)
	{

		Graphics2D gg = (Graphics2D)g;
		
		gg.clearRect(0, 0, getWidth(), getHeight());
		gg.setColor(Color.yellow);
		
		gg.translate(offset, offset);
		gg.fillRect(-3, -3, 8*a+6, 9*a+6);
		gg.setColor(Color.black);
		gg.draw(new Rectangle(-3, -3, 8*a+6, 9*a+6));
		
		for(int i = 0; i < 10; i++)
			gg.drawLine(0, i*a, 8*a, i*a);
		for(int j = 0; j < 9; j++)
		{
			gg.drawLine(j*a, 0, j*a, 4*a);
			gg.drawLine(j*a, 5*a, j*a, 9*a);
		}
		
		//画士走的斜线
		gg.drawLine(0, 0,  0, 9*a);
		gg.drawLine(8*a, 0, 8*a, 9*a);
		gg.drawLine(3*a, 0, 5*a, 2*a);
		gg.drawLine(5*a, 0, 3*a, 2*a);
		gg.drawLine(3*a, 9*a, 5*a, 7*a);
		gg.drawLine(3*a, 7*a, 5*a, 9*a);
		
		//画放棋子的十字架
		mb_drawCross(gg, a, 2*a);
		mb_drawCross(gg, 7*a, 2*a);
		mb_drawCross(gg, a, 7*a);
		mb_drawCross(gg, 7*a, 7*a);
		for(int i = 0; i<5; i++)
			{
				mb_drawCross(gg, 2*i*a, 3*a);
				mb_drawCross(gg, 2*i*a, 6*a);
			}
	
			
		//去掉多余的框架,即不在棋盘内的四个半十字架
		gg.setColor(Color.white);
		gg.fillRect(-1*(offset/2+5), 0, offset/2+5, 9*a);
		gg.fillRect(8*a+4, 0, offset/2+2, 9*a);
		//gg.fillr
		
		gg.setColor(Color.red);
		gg.setFont(new Font("华文隶书", Font.ITALIC | Font.BOLD, 60));
		
		//字:楚河汉界
		gg.drawString("楚河", a, 5*a-20);
		gg.drawString("汉界", 5*a, 5*a-20);
	
		gg.translate(-1*offset, -1*offset);
		
	}//drawBoardLines(Graphics g)
	
	public void putChess(Graphics g)
	{
		((Graphics2D)g).translate(offset-piclength/2, offset-piclength/2);
		for(int i = 1; i < 33; i++) //0, 33 no load
		{
			if(chess[i].alive)
			{
				try
				{
					BufferedImage temp = ImageIO.read(new File(chess[i].src));
					if(chess[i].ax == -1 && chess[i].ay == -1)
						g.drawImage(temp, chess[i].x*a, chess[i].y*a,this);//, arg3)
					else
						g.drawImage(temp, chess[i].ax, chess[i].ay,this);//, arg3)
				}
				catch(java.io.IOException e)
				{
					System.err.println("error when get pic["+i+"]:"+chess[i].src);
				}
			}
		}
		
		((Graphics2D)g).translate(offset-piclength/2, offset-piclength/2);
	}//putChess()
	
	protected void mb_drawCross(Graphics g, int x, int y)
    {//cross every length is 20,then a offset of the center 5.
    	int t = 3;
    	int m = t+piclength/3;
    	g.drawLine(x-t, y-t, x-t, y-m);
    	g.drawLine(x-t, y-t, x-m, y-t);
    	g.drawLine(x+t, y-t, x+t, y-m);
    	g.drawLine(x+t, y-t, x+m, y-t);
    	g.drawLine(x+t, y-t, x+t, y-m);
    	g.drawLine(x-t, y+t, x-t, y+m);
    	g.drawLine(x-t, y+t, x-m, y+t);
    	g.drawLine(x+t, y+t, x+t, y+m);
    	g.drawLine(x+t, y+t, x+m, y+t);
    }//mb_drawCross
   
	
	public static int chooseChessman(int x, int y)
	{
		for(int i = 1; i < 33; i++)
		{
			if(chess[i].alive & chess[i].x == x & chess[i].y == y)
			return i;
		}
		return -1;//if no chess on (x, y) return -1;
	}//chooseChessman
	/*
	public static void main(String[] args) 
	{
		javax.swing.JFrame jf = new javax.swing.JFrame("CChess Box Platfom");
		java.awt.Container c = jf.getContentPane();
		
		jf.setSize(800,600); //remember it's JFrame.setSize() not Container
		c.setPreferredSize(new Dimension(600,800));
		
		ChessBoard cb = new ChessBoard();
		
		c.add(cb);
		jf.setVisible(true);		
	}*/
 
}//classChessBoard

class drawCircle extends Thread
{
	int b = ChessBoard.a;	
	int c = ChessBoard.offset;
	public drawCircle(Graphics g, int x, int y)
	{
		java.awt.Color col = g.getColor();
		int i = 5;
		int a = b/3/5;
		g.setColor(java.awt.Color.blue);
		g.fillOval(x*b-b/2, y*b-b/2, 2*a*i, 2*a*i);
		g.setColor(col);
	/*	for(int i = 0; i < 5; i++)
		{
			g.fillOval(x*b-a, y*b-a, 2*a*i, 2*a*i);
			try
			{
				Thread.sleep(200);
			}
			catch(Exception e)
			{
				System.err.println("err drawCircle");
			}
		}
		
	*/	
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -