Java 如何使用arraylist存储坐标以在固定区域中绘制不同颜色的随机点?

Java 如何使用arraylist存储坐标以在固定区域中绘制不同颜色的随机点?,java,loops,arraylist,Java,Loops,Arraylist,任务是在假定的cookie上随机点绘制芯片,同时计算每个芯片的数量并存储坐标,这样每个芯片就不会移动。每个芯片应为不同颜色,并在鼠标按下时启动 我尝试将坐标放入一个数组、多个数组和一个数组列表,并尝试使用if和for循环。之前的结果是,每次点击都会生成一个不同颜色的芯片(某种程度上是因为有时生成的随机数超出了我指定的范围)。在使用多个数组列表并在其中循环之后,当前的行为是它生成一个网格,并且随着每次单击,计数呈指数增长,一些点有很多重叠,网格中的芯片都有一种颜色,即最后一种要指定的颜色 //把图

任务是在假定的cookie上随机点绘制芯片,同时计算每个芯片的数量并存储坐标,这样每个芯片就不会移动。每个芯片应为不同颜色,并在鼠标按下时启动

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

//把图形代码放在这里 公共空间绘制(图g){

ArrayList坐标=新的ArrayList()

col1=generator.nextInt(256)+0;
col2=generator.nextInt(256)+0;
col3=generator.nextInt(256)+0;
画布最大宽度=800;
画布最大高度=600;
画布宽度=400;
画布高度=200;
int newX=(int)(Math.random()*canvasMaxWidth)+canvasMinWidth;
int newY=(int)(Math.random()*canvasMaxHeight)+canvasMinHeight;
点=新点(newX,newY);
g、 setColor(新颜色(205133,63));
g、 fillOval(canvasMinWidth,canvasMinHeight,400400);
用于(点:坐标){
如果(点y>249和点y<549和点x>449和点x<749){
g、 setColor(新颜色(col1、col2、col3));
g、 圆角(x点,y点,7,7);
}
}             
g、 设置颜色(颜色为黑色);
g、 抽绳(“喷水次数:”+计数,100,80);
}
//响应鼠标按下的代码
public void mousePressed(MouseEvent mouse){
}
预期的结果基本上是一个散斑圆盘,该圆盘在每个散斑不移动的情况下对圆盘上的每个散斑进行计数

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

ArrayList<Point> coordinates = new ArrayList<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);
    }
}
如果您想执行随机位置,可以通过以下方式随机生成:

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);
    }
 }      
intnewx=(int)(Math.random()*(canvasMaxWidth-canvasMinWidth))+canvasMinWidth;
int newY=(int)(Math.random()*(canvasMaxHeight-canvasMinHeight))+canvasMinHeight;
点=新点(newX,newY);
坐标。添加(点);
计数器++;
add((int)(Math.random()*255));
greenList.add((int)(Math.random()*255));
blueList.add((int)(Math.random()*255));
g、 setColor(新颜色(205133,63));
g、 fillOval(canvasMinWidth,canvasMinHeight,400400);
对于(intz=0;z249&&coordinates.get(z).y<549&&coordinates.get(z).x>449&&coordinates.get(z).x<749){
g、 setColor(新颜色(redList.get(z).intValue(),greenList.get(z).intValue(),blueList.get(z).intValue());
g、 fillOval(坐标.get(z).x,坐标.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;
公共类myClass{
静态ArrayList坐标=新ArrayList();
静态ArrayList redList=新建ArrayList();
静态ArrayList绿色列表=新建ArrayList();
静态ArrayList blueList=新ArrayList();
静态整数计数器=0;

Wow这些都是非常有用的建议,我使用计数来计算生成的每个点,但我会尝试你的建议。希望它对我有用。根据你的建议有一个问题。它提示我“点已定义”当为循环选择变量的第二次使用时。我该怎么办?您可能只是无意中再次尝试定义它。是否介意使用代码更新您的问题帖子,以便我可以看到它?已更新。此外,我一直在移动canvas max和min的变量定义,以查看它是否更改了任何内容。问题是:Point Point=新点(newX,newY);然后:for(Point Point:coordinates),其中两个实例都命名为Point。
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);
    }
 }      
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;