ChessPanel.java
上传用户:sjjz88
上传日期:2013-04-10
资源大小:452k
文件大小:6k
- //杨建国:ChessPanel.java
- // Drawing a Chess Map.
- import javax.swing.*;
- import javax.swing.event.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- public class ChessPanel extends JPanel{
- private ImageIcon map; //棋盘背景位图
- private ImageIcon blackchess; //黑子位图
- private ImageIcon whitechess; //白子位图
- private int isChessOn [][]; //棋局图
- private int h; //棋子长
- private int w; //棋子宽
- private int insx; //插入棋子的位置
- private int insy;
- private Point mousePoint; //鼠标当前位置
- private int winer; //谁赢了
- private Point last[]; //缓存上一步棋,为了悔棋
- private int plast=0; //走了几步了,
- public int BLACK_ONE; //1表黑子
- public int WHITE_ONE; //-1表白子
- public int NONE_ONE; //0表无子
- public int N; //棋盘边长
- //winer
- ChessPanel(ImageIcon r_map,ImageIcon r_blackchess,ImageIcon r_whitechess)
- {
- N=15;
- map=new ImageIcon();
- blackchess=new ImageIcon();
- whitechess=new ImageIcon();
- last=new Point[2];
- map=r_map;
- blackchess=r_blackchess;
- whitechess=r_whitechess;
- NONE_ONE=0;
- BLACK_ONE=1;
- WHITE_ONE=-1;
- winer=NONE_ONE;
- isChessOn=new int[N][N];
- h=blackchess.getIconHeight()*(N-1);
- w=blackchess.getIconWidth()*(N-1);
- insx=0;
- insy=0;
- mousePoint=new Point();
-
- }
- public void reset() //重开一局
- {
- winer=NONE_ONE;
- for(int i=0;i<N;i++)
- for(int j=0;j<N;j++)
- {
- isChessOn[i][j]=NONE_ONE;
- }
- repaint();
- }
- public void rollback() //悔一步棋
- {
- for(int i=0;i<2;i++)
- {
- isChessOn[last[i].x][last[i].y]=NONE_ONE;
- }
- }
- public void showMousePos(Point p) //调试用,显示鼠标位置
- {
- int cw;
- cw=h/N;
- mousePoint.x=p.x/cw;
- mousePoint.y=p.y/cw;
- repaint();
- }
- public Point addChess(int x,int y,int type) //下一步棋,参数为插入点像素坐标
- {
- int cw;
- insx=x;
- insy=y;
- cw=h/N;
- if(x<=w+cw/2&&y<=h+cw/2&&isChessOn[x/cw][y/cw]==NONE_ONE&&winer==NONE_ONE)
- {
- isChessOn[x/cw][y/cw]=type;
- // System.out.println("add ("+x/cw+","+y/cw+")ttype="+type);
- last[(plast++)%2]=new Point(x/cw,y/cw);
- }
- else
- {
- System.out.println("add Chess error");
- }
- repaint();
- Point r=new Point(x/cw,y/cw);
- return r;
- }
- public Point addChess(int x,int y,int type,boolean isMatrix)//下一步棋,isMatrix位true参数为插入点棋格坐标,否则参数为插入点像素坐标
- {
- if(isMatrix==true)
- {
- if(x<N&&y<N&&isChessOn[x][y]==NONE_ONE&&winer==NONE_ONE)
- {
- isChessOn[x][y]=type;
- // System.out.println("add ("+x+","+y+")ttype="+type+"isChessOn[][]="+isChessOn[x][y]);
- last[(plast++)%2]=new Point(x,y);
- }
- else
- {
- System.out.println("add Chess error");
- }
- repaint();
- Point r=new Point(x,y);
- return r;
- }
- else
- {
- int cw;
- insx=x;
- insy=y;
- cw=h/N;
- if(x<=w+cw/2&&y<=h+cw/2&&isChessOn[x/cw][y/cw]==NONE_ONE&&winer==NONE_ONE)
- {
- isChessOn[x/cw][y/cw]=type;
- }
- repaint();
- Point r=new Point(x/cw,y/cw);
- return r;
- }
-
- }
- public boolean isSuccess(int type) //判断是否有一方胜利
- {
- int i=0;
- int j=0;
- int i1,j1;
- int successFactor_v=0; //横向五子连珠
- int successFactor_h=0; //纵向五子连珠
- int successFactor_l=0; //左斜线五子连珠
- int successFactor_r=0; //右斜线五子连珠
- for(i=0;i<N;i++)
- for(j=0;j<N;j++)
- {
- successFactor_v=0;
- successFactor_h=0;
- successFactor_l=0;
- successFactor_r=0;
- i1=i;
- j1=j;
- while(i1++<N)
- {
- if(isChessOn[i1-1][j]==type) successFactor_v++;
- else i1=N;
- }
- i1=i;
- j1=j;
- while(j1++<N)
- {
- if(isChessOn[i][j1-1]==type) successFactor_h++;
- else j1=N;
- }
- i1=i;
- j1=j;
- while(i1++<N&&j1++<N)
- {
- if(isChessOn[i1-1][j1-1]==type) successFactor_r++;
- else i1=N;
- }
- i1=i;
- j1=j;
- while(i1++<N&&j1-->=0)
- {
- if(isChessOn[i1-1][j1+1]==type) successFactor_l++;
- else i1=N;
- }
- if(successFactor_v>0||successFactor_h>0||successFactor_l>0||successFactor_r>0)
- // System.out.println(type+"t"+i+"--"+j+"tsv="+successFactor_v+"tsh="+successFactor_h+"tsl="+successFactor_l+"tsr="+successFactor_r);
- if(successFactor_v==5||successFactor_h==5||successFactor_l==5||successFactor_r==5)
- return true;
- }
-
-
- return false;
-
- }
- public void gameOver(int r_winer) //游戏胜负已分
- {
- winer=r_winer;
- }
- public void paint(Graphics g) //画客户区
- {
- super.paint(g);
- paintChessMap(g);
- paintChess(g);
- if(winer==BLACK_ONE)
- {
- g.drawString(new String("Game Over!Black Win!"),500,200);
-
- }
- else if(winer==WHITE_ONE)
- {
- g.drawString(new String("Game Over!White Win!"),500,200);
-
- }
- }
- private void paintChessMap(Graphics g) //画棋盘
- {
- map.paintIcon(this,g,10,10);
- int j;
- g.setColor(Color.BLACK);
- for(j=0;j<N;j++) //画线
- {
- g.drawLine(h/N/2,h/N*j+h/N/2,w-w/N+(N%2)*(h/N/2),h/N*j+h/N/2);
- g.drawLine(w/N*j+h/N/2,h/N/2,w/N*j+h/N/2,h-h/N+(N%2)*(h/N/2));
-
- }
- g.fillRect(w/N*7+h/N/2-3,h/N*7+h/N/2-3,6,6);//画5个黑方块
- g.fillRect(w/N*3+h/N/2-3,h/N*3+h/N/2-3,6,6);
- g.fillRect(w/N*11+h/N/2-3,h/N*3+h/N/2-3,6,6);
- g.fillRect(w/N*3+h/N/2-3,h/N*11+h/N/2-3,6,6);
- g.fillRect(w/N*11+h/N/2-3,h/N*11+h/N/2-3,6,6);
- }
- private void paintChess(Graphics g) //画棋子
- {
- int i,j;
- for(i=0;i<N;i++)
- for(j=0;j<N;j++)
- {
- if(isChessOn[i][j]==BLACK_ONE)
- {
- blackchess.paintIcon(this,g,w/N*i,h/N*j);
- }
- else if(isChessOn[i][j]==WHITE_ONE)
- {
- whitechess.paintIcon(this,g,w/N*i,h/N*j);
- }
- }
- }
- }