用鼠标移动棋子后,我正在尝试重新绘制棋子。move() 方法应在从 MouseEvents 获得实例字段变量后将其接收,并将 Piece 对象从其原始(源)位置移动到其目标。注意 Piece 类是抽象的,包含 7 个不同的子类。然而,mouseevents 已经实现并打印出正确的行和列,并且虚拟件也正在打印,但新位置的对象没有被打印。关于为什么的任何建议?
public class Chess extends JComponent implements MouseListener {
public static final int ROW = 8, COLUMN = 8;
private Piece[][] chessPieces=new Piece[ROWS][COLS];
private int sourceR,destR,sourceC,destC;
public void move () {
chessPieces[destR][destC]=chessPieces[sourceR][sourceC];
//This is not taking the Piece object and moving it to the new place?!
System.out.print("destR is "+destR+"destC IS "+destC+"sourceR is"
+sourceR+ "sourceC is "+sourceC+"\n");
chessPieces[sourceR][sourceC]= new Dummy();//removes original piece leaves blank aka dummy
repaint();
}
public void paintComponent (Graphics h) {
Graphics2D g = (Graphics2D)h;
for(int row = 0; row < ROWS; ++row) {
for(int col = 0; col < COLS; ++col) {
theBoard[row][col].paint(g);
chessPieces[row][col].paint(g);
}
}
}
public void mousePressed(MouseEvent event) {
sourceR=event.getX()/45; //
sourceC=event.getY()/45; //here we get the location of the piece to be MOVED
}
public void mouseReleased(MouseEvent event) {
destR=event.getX()/45; //PLACE IT'S MOVED TO
destC=event.getY()/45; //PLACE IT'S MOVED TO
move();
}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
}