在Java下棋,我想移动棋子
Making a chess game in Java, I want to move the pieces
所以我将图像存储为 JButton 上的 ImageIcon。我希望用户单击他们想要使用的片段的 JButton,然后单击另一个 JButton 将其移动到那里,我该怎么做?
我试过使用 actionListener 来获取 ImageIcon,但事实证明它非常复杂,尤其是因为我有一个 JButton 图像的二维数组。
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
JButton[][] squares = new JButton[8][8];
Border emptyBorder = BorderFactory.createEmptyBorder();
for (int row = 0; row < squares.length; row++) {
for (int col = 0; col < squares[row].length; col++) {
JButton tempButton = new JButton();
tempButton.setBorder(emptyBorder);
tempButton.setSize(64, 64);
squares[row][col] = tempButton;
squares[row][col].addActionListener(actionListener);
panel.add(squares[row][col]);
squares[0][0].setIcon(new ImageIcon(BoardGUI.class.getResource("castle.png"), "castle"));
}
}
试试下面的代码。我不知道在 JButtons 上使用 ImageIcons 的确切代码,但这让我明白了:
JButton pieceToMoveButton = null; //variable that persists between actionPerformed calls
public void actionPerformed(ActionEvent actionEvent)
{
JButton button = (JButton)actionEvent.getSource();
if (pieceToMoveButton == null) //if this button press is selecting the piece to move
{
//save the button used in piece selection for later use
pieceToMoveButton = button;
}
else //if this button press is selecting where to move
{
//move the image to the new button (the one just pressed)
button.imageIcon = pieceToMoveButton.imageIcon
pieceToMoveButton = null; //makes the next button press a piece selection
}
}
不确定这是否是您要查找的内容,但这是将 JButton 的位置移动到另一个位置的一种方法:
现在作为一个例子,假设已经有代码声明和初始化一个 JButton(JButton thatotherbutton = new JButton 等)。将它移动到某个位置可以这样完成:
Rectangle rect = thatotherbutton.getBounds();
xcoordinate = (int)rect.getX();
ycoordinate = (int)rect.getY();
chesspiecebutton.setBounds(xcoordinate, ycoordinate, xlengthofbutton, ylengthofbutton);
使用这些坐标来设置您的 JButton 在单击另一个 JButton 时的新边界(换句话说,位置)。
所以我将图像存储为 JButton 上的 ImageIcon。我希望用户单击他们想要使用的片段的 JButton,然后单击另一个 JButton 将其移动到那里,我该怎么做?
我试过使用 actionListener 来获取 ImageIcon,但事实证明它非常复杂,尤其是因为我有一个 JButton 图像的二维数组。
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
JButton[][] squares = new JButton[8][8];
Border emptyBorder = BorderFactory.createEmptyBorder();
for (int row = 0; row < squares.length; row++) {
for (int col = 0; col < squares[row].length; col++) {
JButton tempButton = new JButton();
tempButton.setBorder(emptyBorder);
tempButton.setSize(64, 64);
squares[row][col] = tempButton;
squares[row][col].addActionListener(actionListener);
panel.add(squares[row][col]);
squares[0][0].setIcon(new ImageIcon(BoardGUI.class.getResource("castle.png"), "castle"));
}
}
试试下面的代码。我不知道在 JButtons 上使用 ImageIcons 的确切代码,但这让我明白了:
JButton pieceToMoveButton = null; //variable that persists between actionPerformed calls
public void actionPerformed(ActionEvent actionEvent)
{
JButton button = (JButton)actionEvent.getSource();
if (pieceToMoveButton == null) //if this button press is selecting the piece to move
{
//save the button used in piece selection for later use
pieceToMoveButton = button;
}
else //if this button press is selecting where to move
{
//move the image to the new button (the one just pressed)
button.imageIcon = pieceToMoveButton.imageIcon
pieceToMoveButton = null; //makes the next button press a piece selection
}
}
不确定这是否是您要查找的内容,但这是将 JButton 的位置移动到另一个位置的一种方法: 现在作为一个例子,假设已经有代码声明和初始化一个 JButton(JButton thatotherbutton = new JButton 等)。将它移动到某个位置可以这样完成:
Rectangle rect = thatotherbutton.getBounds();
xcoordinate = (int)rect.getX();
ycoordinate = (int)rect.getY();
chesspiecebutton.setBounds(xcoordinate, ycoordinate, xlengthofbutton, ylengthofbutton);
使用这些坐标来设置您的 JButton 在单击另一个 JButton 时的新边界(换句话说,位置)。