我试图用我的鼠标移动棋子后重新绘制棋子。 move()方法应该在从MouseEvents获取实例字段变量后将其从原始(源)位置移动到其目标位置。注意Piece类是抽象的,包含7个不同的子类。然而,已经实现了鼠标事件并打印出正确的行和颜色,并且还打印了虚拟部件,但是没有打印新位置中的对象。有关为什么的任何建议?
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) {}
}