简体   繁体   English

GUI-国际象棋游戏将碎片拖放到板上“ 2D阵列”

[英]GUI - Chess game drag pieces and drop into a board “ 2D array ”

Hello I'm trying to make a chess game, so far I have created my pieces and I can move them with my mouse. 您好,我正在尝试制作国际象棋游戏,到目前为止,我已经创建了棋子,并且可以用鼠标移动它们。

Now I'm trying to make the board with a 2D array that contains chess piece, so that I when I drag a piece on the board, it adds the piece in the array for example on the image 现在,我尝试使用包含棋子的2D阵列制作棋盘,以便我在棋盘上拖动棋子时将其添加到数组中,例如图像上

I drag the piece to (2,3) and board[2][3] = pawn 我将棋子拖到(2,3)board[2][3] = pawn

例

But I'm not sure how to implement it, I thought about using coordinates like when I drag it into the middle, say I have a frame size of 800x800 and board size of 8 so when I drag my piece to the coordinate (400,400) , board[4][4] = pawn , but then I have to do it for each cell and I'm gonna end up with up 64 if conditions, is there some kind of trick to do it or is my approach wrong? 但是我不确定如何实现它,我想过使用像将其拖动到中间时那样的坐标,说我的帧尺寸为800x800,板尺寸为8,所以当我将其拖动到坐标(400,400)board[4][4] = pawn ,但是然后我必须为每个单元格做它,如果有条件的话,我将得到64,是否有某种技巧可以做到?还是我的方法错误?

If( piece's position is between ... and ... ){
then put into board[0][1]}

If ( piece's position is between ... ) {
then put then put into board[1][1]} 

You could use a mouseListener on the JLabels you have in your board! 您可以在开发板上的JLabel上使用mouseListener! First, build your board by using 8*8 (chess is 8*8, right?) JLabels, store them in some array. 首先,使用8 * 8(chess为8 * 8,对吗?)JLabel来构建板,并将它们存储在某个数组中。

JLabel[][] boardFields = new JLabel[8][8];

You pack these in a JPanel which has GridBagLayout . 您可以将它们打包在具有GridBagLayout的JPanel中。 You can lay them out in the desired pattern pretty easily by using the GridBagContraints class'v gridx and gridy variables. 您可以使用GridBagContraints v gridxgridy变量轻松地将它们按所需的样式gridy

Now what you do is make a static variable somewhere, lets call it selectedPiece . 现在,您要做的是在某个地方创建一个静态变量,让我们将其称为selectedPiece Lets add the mouse listeners to all of our field labels: 让我们将鼠标侦听器添加到我们所有的字段标签中:

for(int i=0;i<8;i++){
   for(int j=0;j<8;j++){

      boardFields[i][j] = new JLabel();
      //set its background white or black here

      //each field will listen to a mouse press (means we selected this piece)
      //and a mouse release (meaning we placed the selected piece here)
      boardFields[i][j].addMouseListener(new MouseAdapter(){

           public void mousePressed(MouseEvent e){
                selectedPiece = //set piece on this field somehow
                //update the background to plain black or white
                //make the icon of the piece follow the cursor
           }

           public void mousePressed(MouseEvent e){
                //update the background to contain the selectedPiece
                //make the icon of the piece stop followin the cursor
                 selectedPiece = null //de-select the piece since we just placed it
           }
      )};
   }
}

This is just a sketch obviously, but it should give you the idea! 显然,这只是一个草图,但是应该可以给您带来灵感!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM