問題描述
連接 4 Android Studio 繪製空格 (Connect 4 Android Studio Draw empty spaces)
我正在為 Android Studio 編寫一個 Connect 4 應用程序,但我無法在板上繪製空白區域,我當前的代碼僅繪製第一行的空白區域。我想實現一個嵌套循環,但我無法在所有行上繪製空格。將 cellSize 添加到 rowPosition 會使圓圈在下方繪製一行。
private void drawEmptyDiscs(Canvas canvas) {
float rowPosition = boardStartY;
canvas.save();
for (int j = 0; j < NUM_OF_ROWS; j++) {
canvas.translate(marginLeft, rowPosition);
paint.setColor(Color.WHITE);
for (int i = 0; i < NUM_OF_COLUMNS; i++) {
// FIXME: if column isn't full? E.g., if (board.isColumnOpen(i)) {
float x = i * cellSize;
float y = cellPadding;
canvas.drawOval(x + cellPadding, y,
x + cellSize ‑ cellPadding, cellSize ‑ cellPadding,
paint);
}
}
canvas.restore();
}
參考解法
方法 1:
You have to update y regarding to j, like:
float y = j * cellSize;
You forgot to add y to the last parameter for the oval. Draw it like:
canvas.drawOval(x + cellPadding, y + cellPadding,
x + cellSize ‑ cellPadding, y + cellSize ‑ cellPadding,
paint);