Código:
public void caballo(final int row, final int column) {
final JButton current = mesa[row][column];
current.setIcon(image);
panel.repaint();
acciones(row, column, current);
}
public void acciones(final int row, final int column, final JButton current) {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
mesa[i][j].addActionListener(e(row, column, current));
}
}
}
public ActionListener e(final int row, final int column,
final JButton current) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
quitarAccion(row, column, current);
if (tienebotton(row + 2, column + 1)) {
if (e.getSource() == mesa[row + 2][column + 1]) {
current.setIcon(null);
caballo(row + 2, column + 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row + 2, column - 1)) {
if (e.getSource() == mesa[row + 2][column - 1]) {
current.setIcon(null);
caballo(row + 2, column - 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 2, column - 1)) {
if (e.getSource() == mesa[row - 2][column - 1]) {
current.setIcon(null);
caballo(row - 2, column - 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 2, column + 1)) {
if (e.getSource() == mesa[row - 2][column + 1]) {
current.setIcon(null);
caballo(row - 2, column + 1);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row + 1, column + 2)) {
if (e.getSource() == mesa[row + 1][column + 2]) {
current.setIcon(null);
caballo(row + 1, column + 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 1, column + 2)) {
if (e.getSource() == mesa[row - 1][column + 2]) {
current.setIcon(null);
caballo(row - 1, column + 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row + 1, column - 2)) {
if (e.getSource() == mesa[row + 1][column - 2]) {
current.setIcon(null);
caballo(row + 1, column - 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
if (tienebotton(row - 1, column - 2)) {
if (e.getSource() == mesa[row - 1][column - 2]) {
current.setIcon(null);
caballo(row - 1, column - 2);
((AbstractButton) e.getSource()).setEnabled(false);
}
}
}
};
}
public boolean tienebotton(int row, int column) {
return (row >= 0 && row < HEIGHT && column >= 0 && column < WIDTH);
}
public void quitarAccion(final int row, final int column, final JButton current) {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
mesa[i][j].removeActionListener(e(row, column, current));
}
}
}
}