有没有办法在 java 中的 10x10 grid/board 中随机化特定颜色?
Is there a way to randomize specific colors across a 10x10 grid/board in java?
目前,我的代码运行是为了创建一个 10x10 的棋盘,每个方块的颜色都是随机的,但我想要它做的是让它在整个棋盘上随机化特定颜色(红色、绿色、蓝色、黄色)。
public static class TestPane extends JPanel {
protected static final int ROWS = 10;
protected static final int COLS = 10;
protected static final int BOX_SIZE = 50;
private List<Color> colors;
public TestPane() {
int length = ROWS * COLS;
colors = new ArrayList<>(length);
for (int index = 0; index < length; index++) {
int c1 = (int) (Math.random() * 255);
int c2 = (int) (Math.random() * 255);
int c3 = (int) (Math.random() * 255);
colors.add(new Color(c1, c2, c3));
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;
System.out.println("...");
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
int index = (row * COLS) + col;
g2d.setColor(colors.get(index));
g2d.fillRect(xOffset + (col * BOX_SIZE),
yOffset + (row * BOX_SIZE),
BOX_SIZE, BOX_SIZE);
}
}
g2d.dispose();
}
非常感谢任何帮助。
制作一个长度与棋盘上的字段一样多的所需颜色数组,然后用 Collections.shuffle(list);
打乱该数组,然后将打乱的颜色数组应用到棋盘上。
要在洗牌前制作第一个数组,请像这样重复颜色:[red, green, blue, yellow, red, green, blue, yellow, red, green, blue, yellow, ...]
首先不要让你的 class static
.
其次,不要处理你的 graphics
对象。
接下来,对于您的特定情况,您可以拥有一组可用颜色:
private Color[] availableColors = new Color[] {Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN};
然后用那里的随机颜色填充你的 colors
ArrayList
int length = ROWS * COLS;
colors = new ArrayList<Color>();
for (int index = 0; index < length; index++) {
int randomColor = (int) (Math.random() * availableColors.length);
colors.add(availableColors[randomColor]);
}
下次,不要忘记在您的问题代码中添加 main
方法。
没有很多特定的颜色,但您可以尝试使用类似的颜色:
Random r = new Random();
Color c = new Color(r.nextInt(3)*127, r.nextInt(3)*127, r.nextInt(3)*127);
这应该会给您更具体的颜色。如果您希望它们更具体(但数量更少),请使用 r.nextInt(1)*255
.
目前,我的代码运行是为了创建一个 10x10 的棋盘,每个方块的颜色都是随机的,但我想要它做的是让它在整个棋盘上随机化特定颜色(红色、绿色、蓝色、黄色)。
public static class TestPane extends JPanel {
protected static final int ROWS = 10;
protected static final int COLS = 10;
protected static final int BOX_SIZE = 50;
private List<Color> colors;
public TestPane() {
int length = ROWS * COLS;
colors = new ArrayList<>(length);
for (int index = 0; index < length; index++) {
int c1 = (int) (Math.random() * 255);
int c2 = (int) (Math.random() * 255);
int c3 = (int) (Math.random() * 255);
colors.add(new Color(c1, c2, c3));
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(COLS * BOX_SIZE, ROWS * BOX_SIZE);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int xOffset = (getWidth() - (COLS * BOX_SIZE)) / 2;
int yOffset = (getHeight() - (ROWS * BOX_SIZE)) / 2;
System.out.println("...");
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
int index = (row * COLS) + col;
g2d.setColor(colors.get(index));
g2d.fillRect(xOffset + (col * BOX_SIZE),
yOffset + (row * BOX_SIZE),
BOX_SIZE, BOX_SIZE);
}
}
g2d.dispose();
}
非常感谢任何帮助。
制作一个长度与棋盘上的字段一样多的所需颜色数组,然后用 Collections.shuffle(list);
打乱该数组,然后将打乱的颜色数组应用到棋盘上。
要在洗牌前制作第一个数组,请像这样重复颜色:[red, green, blue, yellow, red, green, blue, yellow, red, green, blue, yellow, ...]
首先不要让你的 class static
.
其次,不要处理你的 graphics
对象。
接下来,对于您的特定情况,您可以拥有一组可用颜色:
private Color[] availableColors = new Color[] {Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN};
然后用那里的随机颜色填充你的 colors
ArrayList
int length = ROWS * COLS;
colors = new ArrayList<Color>();
for (int index = 0; index < length; index++) {
int randomColor = (int) (Math.random() * availableColors.length);
colors.add(availableColors[randomColor]);
}
下次,不要忘记在您的问题代码中添加 main
方法。
没有很多特定的颜色,但您可以尝试使用类似的颜色:
Random r = new Random();
Color c = new Color(r.nextInt(3)*127, r.nextInt(3)*127, r.nextInt(3)*127);
这应该会给您更具体的颜色。如果您希望它们更具体(但数量更少),请使用 r.nextInt(1)*255
.