英文:
How to display an 8x8 checkerboard?
问题
Color color = Color.gray;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (color == Color.gray) {
color = Color.lightGray;
} else {
color = Color.gray;
}
g.setColor(color);
g.fillRect(row * 80, col * 80, 80, 80);
}
}
![enter image description here][1]
<details>
<summary>英文:</summary>
I want to display an 8x8 checkerboard, but with the bellow code I only get horizontal lines with alternating colors.
Color color = Color.gray;
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
if (color == Color.gray)
{
color = Color.lightGray;
}
else
{
color = Color.gray;
}
g.setColor(color);
g.fillRect(row*80, col*80, 80, 80);
}
}
![enter image description here][1]
[1]: https://i.stack.imgur.com/P1RbU.png
</details>
# 答案1
**得分**: 0
你逐列创建你的棋盘,每一列垂直排列。
你为每个格子切换颜色,没有错。
在列的结尾,你得到的颜色与你开始列时的颜色不同,这是正确的。
然后你切换颜色并开始下一列,这意味着你用与前一列相同的颜色开始下一列。也就是说,你总是在同一行中使用相同的颜色。
要解决这个问题,可以在每一列之后再切换一次,或者不要在每列的第一个格子切换。
<details>
<summary>英文:</summary>
You create your board columns-wise, each column going vertically.
You toggle the color for each field, not wrong.
At the end of the column, you end up with a different color than you started the column with, which is correct.
Then you toggle the color and start the next column, which means you start the next column with the same color you started the preceding one with. I.e. you always use the same color in the same line.
To solve either toggle once more after each column, or do not toggle for the first field of each column.
</details>
# 答案2
**得分**: 0
你对每条竖直线使用了相同的图案(即从相同的颜色开始并切换)。
BBBBBBBB
WWWWWWWW
BBBBBBBB
WWWWWWWW
BBBBBBBB
WWWWWWWW
BBBBBBBB
WWWWWWWW
但你需要切换每条竖直线的起始,以使连续两条竖直线的起始颜色不同。
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
因此,在你的代码中需要在每一行结束后也改变颜色,因为每一行结束的颜色和下一行开始的颜色是相同的。
```java
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (color == Color.gray) {
color = Color.lightGray;
} else {
color = Color.gray;
}
g2.setColor(color);
g2.fillRect(row * 80, col * 80, 80, 80);
}
if (color == Color.gray) {
color = Color.lightGray;
} else {
color = Color.gray;
}
}
英文:
You are using the same pattern( means start with same color and toggle) for every vertical line.
BBBBBBBB
WWWWWWWW
BBBBBBBB
WWWWWWWW
BBBBBBBB
WWWWWWWW
BBBBBBBB
WWWWWWWW
But you need to toggle the start of every vertical line so that start of two consecutive vertical lines are not the same.
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
So in your code need to change the color for every row also, since after every row end color and next row start color is the same.
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (color == Color.gray) {
color = Color.lightGray;
} else {
color = Color.gray;
}
g2.setColor(color);
g2.fillRect(row * 80, col * 80, 80, 80);
}
if (color == Color.gray) {
color = Color.lightGray;
} else {
color = Color.gray;
}
}
答案3
得分: 0
一种典型的做法是根据列索引的值来设置“Color”。也就是说,如果索引是偶数,就使用一种“Color”,如果是奇数,则根据列使用另一种“Color”。
Color color = col % 2 == 0 ? Color.GRAY : Color.LIGHT_GRAY;
g.setColor(color);
然而,你可能会遇到同样的问题,因为这样只会在列之间交替使用颜色。当你开始新的行时,会得到与上面列相同的颜色,因此会看到相同颜色的垂直列。
因此,解决方案是在确定颜色时同时使用行索引和列索引:
Color color = (row + col) % 2 == 0 ? Color.GRAY : Color.LIGHT_GRAY;
g.setColor(color);
此外,注意应该使用大写的“Color”变量,因为它们遵循命名“static final”变量的约定。小写变量只是为了向后兼容,随时可能被移除。
英文:
A typical way to do this is to set the Color
based on the value of the column index. That is, if the index is even you use one Color
, if it is odd you use another Color
based on the column.
Color color = col % 2 == 0 ? Color.GRAY : Color.LIGHT_GRAY;
g.setColor(color);
However, you will have the same problem since this will just alternate column values. When you start a new row, you will get the same Color as the column above so you see vertical columns of the same Color.
So the solution is to use both row/column index when determining the Color:
Color color = (row + col) % 2 == 0 ? Color.GRAY : Color.LIGHT_GRAY;
g.setColor(color);
Also, note that you should be use the upper case Color variables since they follow the convention for naming "static final" variables. The lower case variables only exist for backward compatibility and could be removed at any time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论