在java中,有没有一种方法可以在10x10网格/电路板上随机分配特定的颜色?
目前,我的代码运行时会创建一个10x10的电路板,每个方块都有随机的颜色,但我希望它能在整个电路板上随机选择特定的颜色(红、绿、蓝、黄)在java中,有没有一种方法可以在10x10网格/电路板上随机分配特定的颜色?,java,swing,jframe,Java,Swing,Jframe,目前,我的代码运行时会创建一个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;
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();
}
publicstaticclasstestpane扩展了JPanel{
受保护的静态最终整数行=10;
受保护静态最终整数=10;
受保护的静态最终输入框尺寸=50;
私有列表颜色;
公共测试窗格(){
整数长度=行*列;
颜色=新阵列列表(长度);
for(int index=0;index
非常感谢您的帮助。制作一个所需颜色的数组,其长度与棋盘上的字段长度相同,然后使用
集合对该数组进行洗牌。洗牌(列表);
,然后将洗牌后的颜色数组应用于棋盘
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();
}
要在洗牌之前创建第一个数组,请重复如下颜色:
[红、绿、蓝、黄、红、绿、蓝、黄、红、绿、蓝、黄,…]
首先不要使类处于静态状态
其次,不要处理图形
对象
接下来,对于您的特定情况,您可以拥有一系列可用颜色:
private Color[] availableColors = new Color[] {Color.YELLOW, Color.RED, Color.BLUE, Color.GREEN};
然后用随机颜色填充颜色
数组列表
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]);
}
int length=行*COLS;
颜色=新的ArrayList();
for(int index=0;index
下次,别忘了在你的问题代码中添加一个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