如何使用MouseListener。不工作GUI

时间:2015-05-16 12:17:21

标签: java swing user-interface chess

我正在尝试根据本教程编写简单的国际象棋应用程序:https://proghammer.wordpress.com/2010/08/10/chess01-dragging-game-pieces/ 我使用此代码进行了一些更改,但它不起作用。我是Java GUI编程的新手,有人能告诉我我做错了什么。

更新: 我试图纠正我的错误,当我问这个问题时,我试着调试这个。我认为问题出在ChessGame级。像mouseDragged()这样的方法正在运行,但没有任何改变。碎片没有拖动。我要求帮助解决这个问题。 代码太多,但此代码仅用于提供完整信息。我再说一遍,我认为问题出在ChessGame或(可能在ChessView中),其他代码仅用于理解结构。

这是我的代码:

public class ChessView extends JPanel {

ChessBoard board;

private static final int BOARD_START_X = 220;
private static final int BOARD_START_Y = 120;

private static final int TILE_WIDTH = 45;
private static final int TILE_HEIGHT = 45;

private Image background;

public ChessView() {
    board = new ChessBoard();
    // load and set background image
    URL backgroundImg = getClass().getClassLoader().getResource("background.jpg");
    this.background = new ImageIcon(backgroundImg).getImage();


    // add mouse listeners to enable drag and drop
    ChessGame game = new ChessGame(board, this);
    this.addMouseListener(game);
    this.addMouseMotionListener(game);

    // create application frame and set visible
    //
    JFrame f = new JFrame();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(this);
    f.setResizable(false);
    f.setSize(this.background.getWidth(null), this.background.getHeight(null));
}

public static void main(String[] args) {
    new ChessView();
}

@Override
protected void paintComponent(Graphics g) {
    g.drawImage(this.background, 0, 0, null);
    int row;
    for(int i = 0; i < board.getHeight(); i++) {
        for(int j = 0; j < board.getWidth(); j++) {
            Piece piece = board.getBoardAsArray()[i][j];
            if (piece.getClass() != EmptySquare.class) {
                if (piece.getColor() == other.Color.WHITE) {
                    if (piece.getClass() != Pawn.class) row = 7;
                    else row = 6;
                } else {
                    if (piece.getClass() != Pawn.class) row = 0;
                    else row = 1;
                }
                int x = BOARD_START_X + (TILE_WIDTH * j);
                int y = BOARD_START_Y + (TILE_HEIGHT * row);
                piece.setX(x);
                piece.setY(y);
                g.drawImage(piece.getImage(), x, y, null);

            }
        }
    }
}

public ChessBoard getBoard() {
    return board;
}
}

这是我的听众:

public class ChessGame implements MouseListener, MouseMotionListener {

private ChessBoard board;
private ChessView view;

private Piece dragPiece;
private int dragOffsetX;
private int dragOffsetY;


public ChessGame(ChessBoard board, ChessView chessGui) {
    this.board = board;
    this.view = chessGui;
}

@Override
public void mousePressed(MouseEvent e) {
    int x = e.getPoint().x;
    int y = e.getPoint().y;

    for(int i = 0; i < view.getBoard().getHeight(); i++) {
        for(int j = 0; j < view.getBoard().getWidth(); j++) {
            Piece piece = view.getBoard().getBoardAsArray()[i][j];
            if (piece.getClass() != EmptySquare.class) {
                if (mouseOverPiece(piece, x, y)) {
                    this.dragOffsetX = x - piece.getX();
                    this.dragOffsetY = y - piece.getY();
                    this.dragPiece = piece;
                    break;
                }
            }
        }
    }
}

/**
 * check whether the mouse is currently over this piece
 * @param piece the playing piece
 * @param x x coordinate of mouse
 * @param y y coordinate of mouse
 * @return true if mouse is over the piece
 */
private boolean mouseOverPiece(Piece piece, int x, int y) {
    return piece.getX() <= x
            && piece.getX()+piece.getWidth() >= x
            && piece.getY() <= y
            && piece.getY()+piece.getHeight() >= y;
}

@Override
public void mouseReleased(MouseEvent arg0) {
    this.dragPiece = null;
}

@Override
public void mouseDragged(MouseEvent evt) {
    if(this.dragPiece != null){
        this.dragPiece.setX(evt.getPoint().x - this.dragOffsetX);
        this.dragPiece.setY(evt.getPoint().y - this.dragOffsetY);
        this.view.repaint();
    }

}

@Override
public void mouseClicked(MouseEvent arg0) {}

@Override
public void mouseEntered(MouseEvent arg0) {}

@Override
public void mouseExited(MouseEvent arg0) {}

@Override
public void mouseMoved(MouseEvent arg0) {}

}

这是我的董事会代表:

public class ChessBoard {
private Piece[][] board = new Piece[8][8];

private final int WIDTH = 8;
private final int HEIGHT = 8;

//Write a NullPiece class or contain nulls is 2-5 rows?
public ChessBoard () {
    setStartBoard(); //rewrite
}

public int getHeight() {
    return HEIGHT;
}

public int getWidth() {
    return WIDTH;
}

public Piece[][] getBoardAsArray() {
    return board;
}

public ArrayList<Piece> getBoardAsArrayList() {
    ArrayList<Piece> list = new ArrayList<Piece>();
     for (int i = 0; i < WIDTH; i++) {
         for (int j = 0; j < HEIGHT; j++) {
             list.add(board[i][j]);
         }
     }
    return list;
}

Piece firstWhiteRook = new Rook(Color.WHITE);
Piece secondWhiteRook = new Rook(Color.WHITE);
Piece firstWhiteKnight = new Knight(Color.WHITE);
Piece secondWhiteKnight = new Knight(Color.WHITE);
Piece firstWhiteBishop = new Bishop(Color.WHITE);
Piece secondWhiteBishop = new Bishop(Color.WHITE);
Piece whiteQueen = new Queen(Color.WHITE);
Piece whiteKing = new King(Color.WHITE);
Piece firstWhitePawn = new Pawn(Color.WHITE);
Piece secondWhitePawn = new Pawn(Color.WHITE);
Piece thirdWhitePawn = new Pawn(Color.WHITE);
Piece fourthWhitePawn = new Pawn(Color.WHITE);
Piece fifthWhitePawn = new Pawn(Color.WHITE);
Piece sixthWhitePawn = new Pawn(Color.WHITE);
Piece seventhWhitePawn = new Pawn(Color.WHITE);
Piece eighthWhitePawn = new Pawn(Color.WHITE);

Piece firstBlackRook = new Rook(Color.BLACK);
Piece secondBlackRook = new Rook(Color.BLACK);
Piece firstBlackKnight = new Knight(Color.BLACK);
Piece secondBlackKnight = new Knight(Color.BLACK);
Piece firstBlackBishop = new Bishop(Color.BLACK);
Piece secondBlackBishop = new Bishop(Color.BLACK);
Piece blackQueen = new Queen(Color.BLACK);
Piece blackKing = new King(Color.BLACK);
Piece firstBlackPawn = new Pawn(Color.BLACK);
Piece secondBlackPawn = new Pawn(Color.BLACK);
Piece thirdBlackPawn = new Pawn(Color.BLACK);
Piece fourthBlackPawn = new Pawn(Color.BLACK);
Piece fifthBlackPawn = new Pawn(Color.BLACK);
Piece sixthBlackPawn = new Pawn(Color.BLACK);
Piece seventhBlackPawn = new Pawn(Color.BLACK);
Piece eighthBlackPawn = new Pawn(Color.BLACK);


//TODO: make this more beautiful?
public void setStartBoard() {

    //set color for each piece

    //add all pieces to board
    board[0][0] = firstBlackRook;
    board[0][1] = firstBlackKnight;
    board[0][2] = firstBlackBishop;
    board[0][3] = blackQueen;
    board[0][4] = blackKing;
    board[0][5] = secondBlackBishop;
    board[0][6] = secondBlackKnight;
    board[0][7] = secondBlackRook;

    board[1][0] = firstBlackPawn;
    board[1][1] = secondBlackPawn;
    board[1][2] = thirdBlackPawn;
    board[1][3] = fourthBlackPawn;
    board[1][4] = fifthBlackPawn;
    board[1][5] = sixthBlackPawn;
    board[1][6] = seventhBlackPawn;
    board[1][7] = eighthBlackPawn;


    board[7][0] = firstWhiteRook;
    board[7][1] = firstWhiteKnight;
    board[7][2] = firstWhiteBishop;
    board[7][3] = whiteQueen;
    board[7][4] = whiteKing;
    board[7][5] = secondWhiteBishop;
    board[7][6] = secondWhiteKnight;
    board[7][7] = secondWhiteRook;

    board[6][0] =  firstWhitePawn;
    board[6][1] =  secondWhitePawn;
    board[6][2] =  thirdWhitePawn;
    board[6][3] =  fourthWhitePawn;
    board[6][4] =  fifthWhitePawn;
    board[6][5] =  sixthWhitePawn;
    board[6][6] =  seventhWhitePawn;
    board[6][7] = eighthWhitePawn;

    for(int i = 2; i < 6; i++) {
        for(int j = 0; j < 8; j++) {
            board[i][j] = new EmptySquare(Color.EMPTY);
        }
    }


}

}

这是我的作品实施:

public class PieceImpl implements Piece {

private int x;

private int y;

private static final String NAME = ""; // Only for getName(). Don't use.

private Color color;

private Image image;

public PieceImpl(Color color) {
    this.color = color;
}

public void setColor(Color color) {
    this.color = color;
}

public Color getColor() {
    return color;
}

public Image getImage() {
    return image;
}

public void setImage(Image image) {
    this.image = image;
}

public String toString() {
    if (this.getColor() == Color.BLACK) {
        return "b" + NAME;
    } else {
        return "w" + NAME;
    }
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getWidth() {
    return image.getWidth(null);
}

public int getHeight() {
    return image.getHeight(null);
}
}

这是执行王的例子:

public class King extends PieceImpl implements Piece{
private static final String NAME = "K";

public King(Color color) {
    super(color);
    if (this.getColor() == Color.WHITE) {
        URL urlToImage = getClass().getClassLoader().getResource("wk.png");
        setImage(new ImageIcon(urlToImage).getImage());
    } else if (this.getColor() == Color.BLACK) {
        URL urlToImage = getClass().getClassLoader().getResource("bk.png");
        setImage(new ImageIcon(urlToImage).getImage());
    }
}

public String toString() {
    if (this.getColor() == Color.BLACK) {
        return "b" + NAME;
    } else {
        return "w" + NAME;
    }
}

我用枚举类表示颜色:

public enum Color {

BLACK("black", 0),
WHITE("white", 1),
EMPTY("empty", -1);

private final String name;
private final int id;

private Color(String name, int id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return this.name;
}

public int getID() {
    return this.id;
}

}

这不仅是我的代码,正如我所说,我正在尝试用教程学习。但我想用我的董事会代表。当我运行应用程序时,它运行,我看到GUI,但鼠标事件不起作用。有人能告诉我在哪里犯了错误,为什么鼠标事件不起作用?

1 个答案:

答案 0 :(得分:1)

我从你的评论和调试信息中感觉到你的mouseListener工作正常。所以问题出在其他地方。研究你的代码我发现你的ChessView课程&#39; paintComponent方法会在代码中重置piece变量xy

int x = BOARD_START_X + (TILE_WIDTH * j);
int y = BOARD_START_Y + (TILE_HEIGHT * row);
piece.setX(x);
piece.setY(y);

并将其绘制在位置xy的棋盘上。所以这基本上取消了你在mouseListener中所做的事情:

this.dragPiece.setX(evt.getPoint().x - this.dragOffsetX);
this.dragPiece.setY(evt.getPoint().y - this.dragOffsetY);

因此,您应该可以设置piece班级中的board&#39;新位置

OR

您应该有一个系统来存储每个部分的xy的初始值,并且不应重置{{1}中的xy值并且应该调用

paintComponent
相关问题