【发布时间】:2019-08-28 03:22:10
【问题描述】:
我使用 java 创建了一个跳棋游戏界面,并将棋子放置在游戏板上的每个位置,但目前我无法将棋子从一个方格移动到另一个方格。
如何在棋盘游戏中移动棋子? 以下是我尝试过的源代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chackergame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
/**
*
* @author wenda
*/
public class CheckerGame extends JFrame {
private final int ROWS = 8;
private final int COLL = 8;
private final JPanel[][] square = new JPanel[8][8];
private JPanel backgroundPanel;
private final JLabel statusBar = new JLabel(" Red turn to play");
private JLabel lbPieces = new JLabel();
private boolean inDrag = false;
public CheckerGame() {
//Add a chess board to the Layered Pane
backgroundPanel = new JPanel();
backgroundPanel.setLayout(new GridLayout(8, 8, 0, 0));
add(backgroundPanel, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
createSquare();
addPieces();
setIconImage(PiecesIcon.iconGame.getImage());
setTitle("Java Checkers");// set title game
setSize(500, 500);
//setResizable(false);
setLocationRelativeTo(null);
setLocationByPlatform(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void createSquare() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLL; col++) {
JPanel cell = new JPanel();
if ((row + col) % 2 == 1) {
cell.setBackground(new Color(99, 164, 54));
// cell.setBackground(Color.BLACK);
}
if ((row + col) % 2 == 0) {
cell.setBackground(new Color(247, 235, 164));
// cell.setBackground(Color.WHITE);
}
square[row][col] = cell;
backgroundPanel.add(square[row][col]);
}
}
}
/**
* Add pieces to the board
*/
private void addPieces() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLL; col++) {
lbPieces = new JLabel();
lbPieces.setHorizontalAlignment(SwingConstants.CENTER);
//lb.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
if ((row + col) % 2 == 1) {
if (row < 3) {
lbPieces.setIcon(PiecesIcon.redPiece);
} else if (row > 4) {
lbPieces.setIcon(PiecesIcon.whitePiece);
}
}
square[row][col].setLayout(new BorderLayout());
square[row][col].add(lbPieces, BorderLayout.CENTER);
} // end of for col loop
} // end of for row loop
} // end of addPieces method
public class MouseInput extends MouseAdapter {
@Override
public void mousePressed(MouseEvent evt) {
}
@Override
public void mouseReleased(MouseEvent evt) {
}
@Override
public void mouseClicked(MouseEvent evt) {
}
@Override
public void mouseDragged(MouseEvent evt) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new CheckerGame();
}
}
【问题讨论】:
-
“下面是我的程序的屏幕截图:file:///home/../Screenshot%20from%202019-08-28%2011-56-51.png” i> 请在提交问题之前查看消息发布/编辑表单下方的预览。
-
为什么有两个if语句来设置板子的背景颜色(黑/白)?颜色不是黑色还是白色,因此您只需要一个带有 else 条件的 if 语句。只是一个让更简单的提示。 如何在棋盘游戏中移动棋子? - 定义“移动”。你想 1) 点击一块然后点击一个正方形,或者 2) 将一块从一个正方形拖到另一个。查看:stackoverflow.com/questions/6811247/… 了解选项 2 的实现。
-
我想通过拖放移动棋盘上的棋子
标签: java swing drag-and-drop