想写个单机的象棋 就是按规则走就就行,不要人机对战,由于太菜,发现问题不少。
首先就是用图片背景做棋盘,小图片做棋子,怎么做监听呢?

解决方案 »

  1.   

    使用button作为棋子,然后把图片作为button的icon,为button添加监听。
      

  2.   

    Button b = new Button();
    b.addActionListener(new MyActionListener());public class MyActionListener implements ActionListener{    public voic actionPerformed(ActionEvent e) {
            //do what do you like
        }}
      

  3.   

    Java组件本身就已经被添加到了监听序列中,只要为其添加监听者addXxxListener,就可以在组件层次处理鼠标等人机互动事件see:class VisualFigure extends JLabel implements MouseListener, MouseMotionListener {
            int rank, file; // The field where this figure should be.
            int mousePressedX, mousePressedY; //Where this figure started
            // to be dragged.
            boolean isWhiteFigure;
            int type;        public VisualFigure(int _type) {
                super((ImageIcon) icons.get(new Integer(_type)));
                type = _type;
                isWhiteFigure = type < 7;
                setSize(getIcon().getIconWidth(), getIcon().getIconHeight());
                addMouseMotionListener(this);
                addMouseListener(this);
            }
            public int getRank() {
                if (reverseBoard)
                    return ((getLocation().y + (FIELDSIZE / 2)) / FIELDSIZE);
                else
                    return 7 - ((getLocation().y + (FIELDSIZE / 2)) / FIELDSIZE);
            }
            public int getFile() {
                if (reverseBoard)
                    return 7 - ((getLocation().x + (FIELDSIZE / 2)) / FIELDSIZE);
                else
                    return ((getLocation().x + (FIELDSIZE / 2)) / FIELDSIZE);
            }
            public void setField(int newRank, int newFile) {
                if (reverseBoard)
                    setLocation((7 - newFile) * FIELDSIZE, newRank * FIELDSIZE);
                else
                    setLocation(newFile * FIELDSIZE, (7 - newRank) * FIELDSIZE);
            }        // The following methods are required for the MouseListener
            // and MouseMotionListener interfaces.
            public void mousePressed(MouseEvent e) {
                if (isLocked)
                    return;
                rank = getRank();
                file = getFile();
                mousePressedX = e.getX();
                mousePressedY = e.getY();
                synchronized (virtualBoard) {
                    if (virtualBoard.field[(rank << 3) + file] == VirtualBoard.EMPTY_FIELD) {
                        VisualBoard.this.update();
                        return;
                    }
                    if (jcb.settings.showPossibleMoves)
                        for (int r = 0; r < 8; r++)
                            for (int f = 0; f < 8; f++)
                                if (virtualBoard.isPossibleMove(new Move(rank, file, r, f)))
                                    if (reverseBoard)
                                        background[r][7 - f].setBackground(jcb.settings.boardHighlightColor);
                                    else
                                        background[7 - r][f].setBackground(jcb.settings.boardHighlightColor);
                }
            }
            public void mouseDragged(MouseEvent e) {
                if (isLocked)
                    return;
                int newX, newY;
                newX = getLocation().x + e.getX() - mousePressedX;
                if (newX > MAX)
                    newX = MAX;
                if (newX < 0)
                    newX = 0;
                newY = getLocation().y + e.getY() - mousePressedY;
                if (newY > MAX)
                    newY = MAX;
                if (newY < 0)
                    newY = 0;
                setLocation(newX, newY);
                boardPane.setLayer(this, 10, 0);
            }
            public void mouseReleased(MouseEvent e) {
                if (isLocked)
                    return;
                synchronized (virtualBoard) {
                    int newRank = getRank();
                    int newFile = getFile();
                    boardPane.setLayer(this, 2, 0);
                    if (rank != newRank || file != newFile) {
                        final Move move = new Move(rank, file, newRank, newFile);
                        if (virtualBoard.isPossibleMove(move)) {
                            if ((virtualBoard.field[move.fromField()] == VirtualBoard.WHITE_PAWN && move.toRank() == 7)
                                || (virtualBoard.field[move.fromField()] == VirtualBoard.BLACK_PAWN && move.toRank() == 0)) {
                                move.setPawnPromotion(selectPromotion());
                            }
                            //isLocked=true;
                            synchronized (jcb) {
                                jcb.makeUsersMove(move);
                            }
                        } else
                            VisualBoard.this.update();
                    } else
                        VisualBoard.this.update();
                }
            }
            public void mouseMoved(MouseEvent e) {
            }
            public void mouseClicked(MouseEvent e) {
            }
            public void mouseExited(MouseEvent e) {
            }
            public void mouseEntered(MouseEvent e) {
            }
        }