如何使用数组列表来存储坐标以在固定区域中绘制不同颜色的随机点?



任务是在随机点在假定的cookie上绘制筹码,同时计算每个筹码并存储坐标,以便每个筹码不会移动。每个芯片应具有不同的颜色,并在按下鼠标时启动。

我试图将坐标放入一个数组中,将多个数组放入单个数组列表中,并尝试使用 if 和 for 循环。之前的结果是,每次点击都会生成一个不同颜色的芯片(因为有时生成的随机数超出了我的指定范围(。在使用并循环浏览多个数组列表后,当前的行为是它生成一个网格,并且每次单击计数呈指数级增长,一些点有很多重叠,网格中的芯片都是一种颜色,即最后一个要分配的颜色

把图形代码放在这里 公共无效抽奖(图形g({

数组列表坐标 = 新的数组列表((;

col1= generator.nextInt(256) + 0;
col2= generator.nextInt(256) + 0;
col3= generator.nextInt(256) + 0;
canvasMaxWidth=800;
canvasMaxHeight=600;
canvasMinWidth=400;
canvasMinHeight=200;
int newX = (int) (Math.random() * canvasMaxWidth) + canvasMinWidth;
int newY = (int) (Math.random() * canvasMaxHeight) + canvasMinHeight;
Point point = new Point(newX, newY);

g.setColor(new Color(205,133,63));
g.fillOval(canvasMinWidth,canvasMinHeight,400,400); 

for(Point point : coordinates){
if(point.y > 249 && point.y < 549 && point.x > 449 && point.x < 749){
g.setColor(new Color(col1, col2, col3));
g.fillOval(point.x, point.y, 7, 7);
}
}             
g.setColor(Color.black); 
g.drawString("Number of Sprinkles: "+ count, 100, 80);  

}
//Code to respond to a mouse press
public void mousePressed(MouseEvent mouse){


}

预期结果本质上是一个斑点圆盘,它计算圆盘上的每个斑点,每个散点不移动。

目前看来,似乎没有一个点在移动,但正在发生的事情并没有太多的随机性。 斑点每次点击与迭代相乘并形成网格。

ArrayList<Point> coordinates = new ArrayList<Point>();

跟踪坐标比使用两个单独的列表要容易得多。一切都以网格格式发生,因为您正在使用 i 和 j 的嵌套 for 循环。

您当前正在执行: 如果 i = 0, 1, 2 且 j = 0 , 1, 2

i  j
0  0
0  1
0  2
1  0
1  1
1  2
2  0
2  1
2  2

因此,您可以看到网格状布局的来源。使用 Point 对象,您可以改为执行以下操作:

for(Point point : coordinates){
if(point.y > 249 && point.y < 549 && point.x > 449 && point.x < 749){
g.setColor(new Color(col1, col2, col3);
g.fillOval(point.x, point.y, 7, 7);
}
}

我不确定你为什么要使用计数器,因为它会在坐标的长度上执行。如果要添加随机性,则可以使用 Random 类动态创建值。

int colorValue = (int) (Math.random() * 255);

如果您想执行随机仓位,您可以通过以下方式随机生成它们:

int newX = (int) (Math.random() * (canvasMaxWidth - canvasMinWidth)) + canvasMinWidth;
int newY = (int) (Math.random() * (canvasMaxHeight - canvasMinHeight)) + canvasMinHeight;
Point point = new Point(newX, newY);

使用您的代码进行编辑:

int newX = (int) (Math.random() * (canvasMaxWidth - canvasMinWidth)) + canvasMinWidth;
int newY = (int) (Math.random() * (canvasMaxHeight - canvasMinHeight)) + canvasMinHeight;
Point point = new Point(newX, newY);
coordinates.add(point);
counter++;
redList.add((int) (Math.random() * 255));
greenList.add((int) (Math.random() * 255));
blueList.add((int) (Math.random() * 255));
g.setColor(new Color(205,133,63));
g.fillOval(canvasMinWidth,canvasMinHeight,400,400); 

for(int z = 0; z < coordinates.size(); z++){
if(coordinates.get(z).y > 249 && coordinates.get(z).y < 549 && coordinates.get(z).x > 449 && coordinates.get(z).x < 749){
g.setColor(new Color(redList.get(z).intValue(), greenList.get(z).intValue(), blueList.get(z).intValue()));
g.fillOval(coordinates.get(z).x, coordinates.get(z).y, 7, 7);
}
}      

编辑#2:这是如何拥有静态变量的示例

public class myClass{
static ArrayList<Point> coordinates = new ArrayList<Point>();
static ArrayList<Integer> redList = new ArrayList<Integer>();
static ArrayList<Integer> greenList = new ArrayList<Integer>();
static ArrayList<Integer> blueList = new ArrayList<Integer>();
static int counter = 0;

最新更新