我创造了经典的国际象棋游戏,并实现了一个棋盘,我创建了一个抽象的棋子类,其他部分扩展。我创建了一个Pawn类,我正在尝试实现并测试移动pawn。我创建了一个内部类,它扩展了鼠标适配器并覆盖了mouseClicked()
方法。我打印1到控制台,以确保它的工作原理。要求是单击“源”部分,然后单击目标,并且该部分应移动到目标,只要它在板的边界内。
我想知道我是否将鼠标监听器放在正确的区域,如果是这样,如果每次鼠标点击事件发生时都执行了mouseClicked方法,你怎么能记住被点击为源的片段,按顺序将其移动到您单击的目标位置。
电路板由背景物体组成,背景物体只是一个“黑色”或“白色”方形图像,它自己绘制,板由背景物体的8 x 8网格组成。代码如下:
Background.java
/*
* Displays a single image of a background
*/
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.File;
public class BackGround {
private BufferedImage image;
private int x,y;
public BackGround(String imageName, int x, int y) {
this.x = x;
this.y = y;
try {
image = ImageIO.read(new File(imageName));
} catch (IOException e) {}
}
public void paint(Graphics2D g) {
g.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
}
}
Board.java
/*
* Displays a board
*/
import java.awt.Graphics2D;
import java.awt.Graphics;
public class Board {
private Piece[][] pieces;
private BackGround[][] theBackGround;
private int ROW_SIZE;
private int COL_SIZE;
private int xPos = 220;
private int yPos = 220;
final protected static String IMG_BG_BLACK = "image/black.PNG";
final protected static String IMG_BG_WHITE = "image/white.PNG";
public Board(final int ROW_SIZE, final int COL_SIZE) {
this.ROW_SIZE = ROW_SIZE;
this.COL_SIZE = COL_SIZE;
pieces = new Piece[ROW_SIZE][COL_SIZE];
for(int i = 0; i < ROW_SIZE; i++) {
for(int j = 0; j < COL_SIZE; j++) {
pieces[i][j] = new DummyPiece();
}
}
theBackGround = new BackGround[ROW_SIZE][COL_SIZE];
for(int i = 0; i < ROW_SIZE; i++) {
for(int j = 0; j < COL_SIZE; j++) {
if(i % 2 == 0){
if(j % 2 == 0){
theBackGround[i][j] = new BackGround(IMG_BG_BLACK, xPos, yPos);
}
else
theBackGround[i][j] = new BackGround(IMG_BG_WHITE, xPos, yPos);
}
if(i % 2 == 1){
if(j % 2 == 1){
theBackGround[i][j] = new BackGround(IMG_BG_BLACK, xPos, yPos);
}
else
theBackGround[i][j] = new BackGround(IMG_BG_WHITE, xPos, yPos);
}
xPos += 45;
}
xPos = 220;
yPos += 45;
}
}
// Ask each background to print itself
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
for(int i = 0; i < ROW_SIZE; i++) {
for(int j = 0; j < COL_SIZE; j++) {
theBackGround[i][j].paint(g2);
}
}
}
}
Piece.java
//All pieces will inherit from this class
public abstract class Piece {
protected String color;
protected String type;
protected int posR;
protected int posC;
protected BufferedImage image;
abstract public void paint(Graphics2D g2);
abstract public String getType();
abstract public String getColor();
/*
This piece can report it's current row
@return int the pieces current row
*/
public int getRowPos() {
return this.posR;
}
/*
This piece can report it's current column
@return int the pieces current column
*/
public int getColPos() {
return this.posC;
}
/*
Sets a new position for this piece
@param x the new row position for this piece
@param y the new column position for this piece
*/
public void setPos(int x, int y) {
posR = x;
posC = y;
}
}
Pawn.java
//Inherits from abstract Piece
public class Pawn extends Piece {
public Pawn(String imageName, String color, int x, int y) {
this.posR = x;
this.posC = y;
this.type = "PAWN";
this.color = color;
try {
this.image = ImageIO.read(new File(imageName));
} catch (IOException e) {}
}
public String getType() {
return this.type;
}
public String getColor() {
return this.color;
}
public void paint(Graphics2D g) {
g.drawImage(image, this.posR, this.posC, image.getWidth(), image.getHeight(), null);
}
}
ChessBoard.java
public class ChessBoard extends Board {
final private static String IMG_PAWNB = "image/bP.PNG";
final private static String IMG_PAWNW = "image/wP.PNG";
private Pawn pawn;
public ChessBoard() {
super(8, 8);
pawn = new Pawn(IMG_PAWNB, "BLACK", 310, 265);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
pawn.paint(g2);
}
}
ChessViewer.java
public class ChessViewer extends JComponent {
private ChessBoard chessBoard;
public ChessViewer() {
chessBoard = new ChessBoard();
addMouseListener(new MouseClickListener());
}
public void paint(Graphics h) {
chessBoard.paint(h);
}
class MouseClickListener extends MouseAdapter {
public void mouseClicked(MouseEvent event){
System.out.println("1");
}
}
}
ChessRunner.java(测试仪)
public class ChessRunner {
private static final int FRAME_WIDTH = 800;
private static final int FRAME_HEIGHT = 800;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Chess");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChessViewer component = new ChessViewer();
frame.add(component);
frame.setVisible(true);
}
}