📄 chess.java
字号:
{
int now_l=ChessPiece[now_ChessPiece][1],now_h=ChessPiece[now_ChessPiece][2];
Graphics g=getGraphics();
if(haveChessPiece(goto_l,goto_h))ChessPiece[ChesssBoard[goto_l][goto_h]][4]=1;
g.drawImage(everybox[now_l][now_h],ltoL(now_l)-a1,htoH(now_h)-a1,this);
g.drawImage(ChessPiece_pic[now_ChessPiece],ltoL(goto_l)-a1,htoH(goto_h)-a1,this);
ChessPiece[now_ChessPiece][1]=goto_l;ChessPiece[now_ChessPiece][2]=goto_h;
ChesssBoard[now_l][now_h]=0;
ChesssBoard[goto_l][goto_h]=now_ChessPiece;
//胜利判断
if(ChessPiece[5][4]==1){
JOptionPane.showMessageDialog(this ,"红方获胜!!\n再来一局吧!!!","Custom Dialog" ,JOptionPane.INFORMATION_MESSAGE,new ImageIcon("v.jpg"));
Chess.chessframe.setVisible(false);
Chess.chessframe=new MyFrame(); Chess.chessframe.show();
}
if(ChessPiece[21][4]==1){
JOptionPane.showMessageDialog(this ,"黑方获胜!!\n再来一局吧!!!","Custom Dialog" ,JOptionPane.INFORMATION_MESSAGE,new ImageIcon("v.jpg"));
Chess.chessframe.setVisible(false);
Chess.chessframe=new MyFrame(); Chess.chessframe.show();
}
//判断该谁走了
if(turn_number!=red) MyFrame.j.setText("==现在该\n==红==\n走!");
else MyFrame.j.setText("==现在该\n==黑==\n走!");
}
public int between(int start_l,int start_h,int end_l,int end_h)
{
int count=0;
if(start_l==end_l)
{
if(start_h>end_h){int t=start_h;start_h=end_h;end_h=t;}
for(int i=start_h+1;i<end_h;i++)
if(ChesssBoard[start_l][i]!=0)count++;
}
else
{
if(start_l>end_l){int t=start_l;start_l=end_l;end_l=t;}
for(int i=start_l+1;i<end_l;i++)
if(ChesssBoard[i][start_h]!=0)count++;
}
return(count);
}
public void unpickout(int unpickout_ChessPiece_l,int unpickout_ChessPiece_h)
{
Graphics g=getGraphics();
g.drawImage(everybox[unpickout_ChessPiece_l][unpickout_ChessPiece_h],ltoL(unpickout_ChessPiece_l)-a1,htoH(unpickout_ChessPiece_h)-a1,this);
g.drawImage(ChessPiece_pic[now_ChessPiece],ltoL(unpickout_ChessPiece_l)-a1,htoH(unpickout_ChessPiece_h)-a1,this);
}
public void pickout(int pickout_ChessPiece_l,int pickout_ChessPiece_h)
{
Graphics g=getGraphics();
setForeground(Color.white);
g.drawLine(ltoL(pickout_ChessPiece_l)-a1,htoH(pickout_ChessPiece_h)-a1,ltoL(pickout_ChessPiece_l)-a1+4,htoH(pickout_ChessPiece_h)-a1);
g.drawLine(ltoL(pickout_ChessPiece_l)-a1,htoH(pickout_ChessPiece_h)-a1,ltoL(pickout_ChessPiece_l)-a1,htoH(pickout_ChessPiece_h)-a1+4);
}
public void movepic(int mouse_l_num,int mouse_h_num)
{
Graphics g=getGraphics();
g.drawImage( everybox[moveChessPiece_l_num][moveChessPiece_h_num],ltoL(moveChessPiece_l_num)-a1,htoH(moveChessPiece_h_num)-a1,this);
if(haveChessPiece(moveChessPiece_l_num,moveChessPiece_h_num)&&ChesssBoard[moveChessPiece_l_num][moveChessPiece_h_num]!=now_ChessPiece)g.drawImage(ChessPiece_pic[ChesssBoard[moveChessPiece_l_num][moveChessPiece_h_num]],ltoL(moveChessPiece_l_num)-a1,htoH(moveChessPiece_h_num)-a1,this);
g.drawImage(ChessPiece_pic[now_ChessPiece],ltoL(mouse_l_num)-a1,htoH(mouse_h_num)-a1,this);
}
public void unmovepic()
{
Graphics g=getGraphics();
g.drawImage( everybox[moveChessPiece_l_num][moveChessPiece_h_num],ltoL(moveChessPiece_l_num)-a1,htoH(moveChessPiece_h_num)-a1,this);
if(haveChessPiece(moveChessPiece_l_num,moveChessPiece_h_num)&&ChesssBoard[moveChessPiece_l_num][moveChessPiece_h_num]!=now_ChessPiece)g.drawImage(ChessPiece_pic[ChesssBoard[moveChessPiece_l_num][moveChessPiece_h_num]],ltoL(moveChessPiece_l_num)-a1,htoH(moveChessPiece_h_num)-a1,this);
g.drawImage(everybox[ChessPiece[now_ChessPiece][1]][ChessPiece[now_ChessPiece][2]],ltoL(ChessPiece[now_ChessPiece][1])-a1,htoH(ChessPiece[now_ChessPiece][2])-a1,this);
g.drawImage(ChessPiece_pic[now_ChessPiece],ltoL(ChessPiece[now_ChessPiece][1])-a1,htoH(ChessPiece[now_ChessPiece][2])-a1,this);
}
public ChessPanel()
{
datainti();
System.out.println("0,0,0,9:"+between(0,0,9,0));
setLayout(null);
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
if(now_ChessPiece!=0)
{
Graphics g=getGraphics();
int mouse_l_pix=e.getX(),mouse_h_pix=e.getY();
if(mouse_l_pix>ltoL(8))mouse_l_pix=ltoL(8);
int mouse_l_num=Ltol(mouse_l_pix);
int mouse_h_num=Ltol(mouse_h_pix);
if(mouse_l_num!=moveChessPiece_l_num||mouse_h_num!=moveChessPiece_h_num)
{
ChessPiece[now_ChessPiece][3]=1;
movepic(mouse_l_num,mouse_h_num);
pickout( mouse_l_num,mouse_h_num);
moveChessPiece_l_num=mouse_l_num;
moveChessPiece_h_num=mouse_h_num;
}
}
}
});
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int x=e.getX(),y=e.getY();
int l_num=Ltol(x),h_num=Htoh(y);
mouse_sign=-mouse_sign;
if(mouse_sign==-1)
{
if(isOK(l_num,h_num))
{
carry(l_num,h_num);
System.out.println("Carrying "+now_ChessPiece+"to"+l_num+h_num);
ChessPiece[now_ChessPiece][3]=0;
unpickout(ChessPiece[now_ChessPiece][1],ChessPiece[now_ChessPiece][2]);
now_ChessPiece=0;
turn_number=-turn_number;
}
else
{
unmovepic();
ChessPiece[now_ChessPiece][3]=0;
unpickout(ChessPiece[now_ChessPiece][1],ChessPiece[now_ChessPiece][2]);
now_ChessPiece=0;
System.out.println("cancel pick out");
}
}
if(mouse_sign==1)
{
if(haveChessPiece(l_num,h_num))
{
now_ChessPiece=ChesssBoard[l_num][h_num];
if(turn_number==red&&isred(now_ChessPiece)||turn_number==blue&&(!isred(now_ChessPiece)))
{
pickout(l_num,h_num);
ChessPiece[now_ChessPiece][3]=1;
moveChessPiece_l_num=l_num;
moveChessPiece_h_num=h_num;
System.out.println("pick out:"+now_ChessPiece);
}
else
{
now_ChessPiece=0;
mouse_sign=-1;
}
}
else
{
mouse_sign=-1;
System.out.println("cancel pick out"+" "+haveChessPiece(l_num,h_num));
}
}
System.out.println("NOW:"+now_ChessPiece);
System.out.println() ;
}
});
}
public void paint(Graphics g)
{
g.drawImage(ChesssBoard_pic,0,0,this);
for(int i=1;i<33;i++)
{
if(ChessPiece[i][4]!=1&&ChessPiece[i][3]!=1)g.drawImage(ChessPiece_pic[i] ,ltoL(ChessPiece[i][1])-a1,htoH(ChessPiece[i][2])-a1,this) ;
}
for(int i=1;i<33;i++)
{
if(ChessPiece[i][3]==1)g.drawImage(ChessPiece_pic[i] ,ltoL(moveChessPiece_l_num)-a1,htoH(moveChessPiece_h_num)-a1,this) ;
}
}
public void updata(Graphics g)
{
paint(g);
}
}
class MyFrame extends JFrame
{
JPanel myPanel=new ChessPanel();
static JLabel j;
public MyFrame()
{
Container ctPane=getContentPane();
Graphics g=getGraphics();
setTitle("Tube's Chinese Chess");
setBounds(0,0,560,680);
//System.out.println("?=============="+d.height+"h-w"+d.width);
addComponentListener(new ComponentAdapter()
{
public void componentMoved(ComponentEvent e)
{
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
ctPane.setLayout(new BorderLayout());
myPanel.setLayout(null);
myPanel.setBackground(Color.red);
// myPanel.setBounds(0,0,550,666);
ctPane.add(myPanel,"Center");//
j=new JLabel("==现在该\n==红==\n走!");
j.setFont(new Font("Serif",Font.ITALIC,40));
j.setVisible(true);
ctPane.add(j,"South");
}
}
public class Chess
{ static JFrame chessframe;
public static void main(String args[])
{
chessframe=new MyFrame();
//chessframe.setVisible(true);
chessframe.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -