我正在尝试使用81个单板单元为9x9尺寸板绘制一块板。
public abstract class BoardCell {
private float x, y;
public BoardCell( float x, float y ) {
this.x = x;
this.y = y;
}
public abstract void drawBoardCell( Canvas canvas, float x, float y, float width, Paint paint );
}
然后我有其他子类Board扩展并实现了BoardCell的所有抽象方法。
我的问题是我可以这样做:
Class Game extends View {
BoardCell[][] myBoard;
for( int i = 0; i < 9; i++ ) {
for( int j = 0; j < 9; j++ ) {
myBoard[i][j] = new Board( x, y );
myBoard.drawBoardCell( canvas, x, y, width, paint );
}
}
}
我的代码适用于BoardCell myBoard,但不适用于BoardCell的2D数组:(
答案 0 :(得分:0)
您需要参考刚刚制作的最新电路板的索引。变化
myBoard.drawBoardCell( canvas, x, y, width, paint );
到
myBoard[i][j].drawBoardCell( canvas, x, y, width, paint );
您还需要将代码包装在方法中并实例化数组。如果您不确定这是如何工作的,请阅读一些Java教程,其中有很多。
答案 1 :(得分:0)
使用此代码,您需要首先实例化数组。你只需定义myBoard。
BoardCell[][] myBoard = new BoardCell [9][9];