刚刚实现了棋子在棋盘上的精确定位问题.本来想再加上一个功能:当点击棋子,但是并没有让其移动时,可以选择另外一个本方棋子继续移动.算法是:定义变量Redgo=1,Bluego=0;Redgo=1时红棋可以移动,=0时不能移动.Bluego=1时蓝棋可以移动,Bluego=0时不能移动.
在鼠标按下的事件中通过startx,starty两变量保存当前棋子的坐标,在鼠标释放事件中,通过endx,endy保存当前棋子坐标.比较前后两次坐标值,如果不等则修改Redgo,Bluego两变量.
但是运行后,有时同一方棋子可以连续走棋,另外一方则不能走棋;而有时候就是正常的,一方走完另一方再走.
别的地方没什么问题主要出现在ChessBoard类中的mouseReleaed(),mousePressed()事件中.代码太长了发不上去,只发了感觉有问题的地方
请各位高手帮忙解决一下.
变量:p[][]保存的是棋盘上各交点的坐标值;p[][].getX(),p[][].getY()取得该交点x,y坐标
int startx,starty在类中已定义并初始化为0;
整个程序可以正常运行,没有语法错误.如对此有兴趣的,我这有源码 加我QQ279075519,注明:象棋游戏
public void mousePressed(MouseEvent e)//鼠标按下时的事件
{ChessPiece source=(ChessPiece)e.getSource();
startx=source.getBounds().x;
starty=source.getBounds().y;//保存当前棋子所在的坐标
}//void mousePressed
public void mouseReleased(MouseEvent e)//鼠标释放时的事件
{Rectangle rect=null;
int m=0,n=0;
ChessPiece piece=null;
piece=(ChessPiece)e.getSource();
rect=piece.getBounds();
e=SwingUtilities.convertMouseEvent(piece,e,this);
if(Redgo==1&&piece.getTeam()==1)//当红方允许走棋且所选的是红棋时
{
for(int i=0;i<9;i++)
{for(int j=0;j<10;j++)
{int x=p[i][j].getX();
int y=p[i][j].getY();
if(rect.contains(x,y))
{ m=i;
n=j;}
}//for
}//for
piece.setLocation(p[m][n].getX()-19,p[m][n].getY()-20);//重新定位所选红棋子
int endx=p[m][n].getX()-19,endy=p[m][n].getY()-20;
if(startx!=endx&&starty!=endy)//当棋子的坐标发生变化时修改Redgo,Bluego,如果没变不修改
{Redgo=0;
Bluego=1;
startx=0;
starty=0;
}//if(startx!=endx&&starty!=endy)
}//if(Redgo==1&&piece.getTeam()==1)
else if(Bluego==1&&piece.getTeam()==2)//当蓝方允许走棋且所选的是蓝棋时
{for(int i=0;i<9;i++)
for(int j=0;j<10;j++)
{int x=p[i][j].getX();
int y=p[i][j].getY();
if(rect.contains(x,y))
{ m=i;
n=j;}
}//for
piece.setLocation(p[m][n].getX()-19,p[m][n].getY()-20);//重新定位所选蓝棋子
int endx=p[m][n].getX()-19,endy=p[m][n].getY()-20;
if(startx!=endx&&starty!=endy)//当棋子的坐标发生变化时修改Redgo,Bluego,如果没变如果没变不修改
{Redgo=1;
Bluego=0;
startx=0;
starty=0;
}//if(startx!=endx&&starty!=endy)
}//if(Bluego)
}//mouseReleased
在鼠标按下的事件中通过startx,starty两变量保存当前棋子的坐标,在鼠标释放事件中,通过endx,endy保存当前棋子坐标.比较前后两次坐标值,如果不等则修改Redgo,Bluego两变量.
但是运行后,有时同一方棋子可以连续走棋,另外一方则不能走棋;而有时候就是正常的,一方走完另一方再走.
别的地方没什么问题主要出现在ChessBoard类中的mouseReleaed(),mousePressed()事件中.代码太长了发不上去,只发了感觉有问题的地方
请各位高手帮忙解决一下.
变量:p[][]保存的是棋盘上各交点的坐标值;p[][].getX(),p[][].getY()取得该交点x,y坐标
int startx,starty在类中已定义并初始化为0;
整个程序可以正常运行,没有语法错误.如对此有兴趣的,我这有源码 加我QQ279075519,注明:象棋游戏
public void mousePressed(MouseEvent e)//鼠标按下时的事件
{ChessPiece source=(ChessPiece)e.getSource();
startx=source.getBounds().x;
starty=source.getBounds().y;//保存当前棋子所在的坐标
}//void mousePressed
public void mouseReleased(MouseEvent e)//鼠标释放时的事件
{Rectangle rect=null;
int m=0,n=0;
ChessPiece piece=null;
piece=(ChessPiece)e.getSource();
rect=piece.getBounds();
e=SwingUtilities.convertMouseEvent(piece,e,this);
if(Redgo==1&&piece.getTeam()==1)//当红方允许走棋且所选的是红棋时
{
for(int i=0;i<9;i++)
{for(int j=0;j<10;j++)
{int x=p[i][j].getX();
int y=p[i][j].getY();
if(rect.contains(x,y))
{ m=i;
n=j;}
}//for
}//for
piece.setLocation(p[m][n].getX()-19,p[m][n].getY()-20);//重新定位所选红棋子
int endx=p[m][n].getX()-19,endy=p[m][n].getY()-20;
if(startx!=endx&&starty!=endy)//当棋子的坐标发生变化时修改Redgo,Bluego,如果没变不修改
{Redgo=0;
Bluego=1;
startx=0;
starty=0;
}//if(startx!=endx&&starty!=endy)
}//if(Redgo==1&&piece.getTeam()==1)
else if(Bluego==1&&piece.getTeam()==2)//当蓝方允许走棋且所选的是蓝棋时
{for(int i=0;i<9;i++)
for(int j=0;j<10;j++)
{int x=p[i][j].getX();
int y=p[i][j].getY();
if(rect.contains(x,y))
{ m=i;
n=j;}
}//for
piece.setLocation(p[m][n].getX()-19,p[m][n].getY()-20);//重新定位所选蓝棋子
int endx=p[m][n].getX()-19,endy=p[m][n].getY()-20;
if(startx!=endx&&starty!=endy)//当棋子的坐标发生变化时修改Redgo,Bluego,如果没变如果没变不修改
{Redgo=1;
Bluego=0;
startx=0;
starty=0;
}//if(startx!=endx&&starty!=endy)
}//if(Bluego)
}//mouseReleased
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货