绘制随机圆圈,将其坐标存储在数组中

时间:2010-12-01 21:58:39

标签: java arrays drawing

这就是我想要完成的功课:设计并实现一个绘制圆圈的程序,每个圆圈的半径和位置随机确定。如果圆圈与任何其他圆圈不重叠,请将该圆圈绘制为黑色。如果圆圈与一个或多个圆圈重叠,请以青色绘制。使用数组存储每个圆的表示,然后确定每个圆的颜色。如果它们的中心点之间的距离小于它们的半径之和,则两个圆重叠。

我非常接近,但我无法弄清楚如何使用sqrt公式来比较重叠的圆的半径,然后以青色重绘该圆。我试图在其他两个帖子中找到这个:drawing random circles, storing their coorindates in an array和这里:draw random circles, first storing the points in an array。我得到了一些指示,所以任何人都可以给我特定的指针,找出如何使我的for循环正确使用Math.sqrt函数,以便比较半径,然后重绘青色的重叠圆?非常感谢你。

更新:我已经让Math.sqrt论坛工作了,但是我无法弄清楚如何构造我的for循环以便只填充重叠的圆圈。我试图使用嵌套的for来做到这一点循环中包含一个布尔值,但这样就可以填满所有的圆圈。感谢您推荐到目前为止。

这是我到目前为止的代码:

 import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.math.*;

public class RandomCircles extends JPanel
{
    private int[] sizeArray = new int [5];  // radius of each circle
    private int[] xArray = new int [5]; //array to store x coordinates of circles
    private int[] yArray = new int [5]; //array to store y coordinates of circles
    private int x1, x2, y1, y2;
    private boolean overlap = false;


    public RandomCircles()
    {
        Random r = new Random();

        for (int i = 0; i<xArray.length; i++){
            //random numbers from 1 to 20;
            xArray[i] = r.nextInt(200) + 1;
        }
        for (int i = 0; i<yArray.length; i++){
            yArray[i] = r.nextInt(200) + 1;
        }
        for (int i = 0; i<sizeArray.length; i++){
            sizeArray[i] = r.nextInt(100) +1;
        }

        setBackground (Color.blue);
        setPreferredSize (new Dimension(300, 200));
    }
    //  generates all of the circles stored in the array.

    public void paintComponent (Graphics page)
    {
        super.paintComponent(page);
        for (int i = 0 ;i<xArray.length; i++) //this is an iterator that draws the circles and checks for overlapping radii
            for (int j = 0 ;j<xArray.length; j++)
            {
                //boolean overlap = false;
                //is supposed to compare radii of circles to check if they overlap
                {
                    if (Math.sqrt((xArray[i]-xArray[j])*(xArray[i]-xArray[j])+(yArray[i]-yArray[j])*(yArray[i]-yArray[j])) >sizeArray[i]-sizeArray[j]);

                    boolean overlap = true;
                    page.fillOval(xArray[i], yArray[i], sizeArray[i], sizeArray[i]);
                    page.setColor (Color.cyan);
                    repaint();


                } //draws the circles that are stored in the array
                page.drawOval(xArray[i], yArray[i], sizeArray[i], sizeArray[i]);//outer for loop
            }
    }
    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Circles");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add (new RandomCircles());

        frame.pack();
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

//Math.sqrt((x1-x2)*(x1-x2)-(y1-y2)*(y1-y2)), go back and read chapter 7

应该是

Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))

(你需要取平方X和Y距离的的平方根,而不是差异。)

<强>更新

重叠检测存在一些问题。两个圆圈重叠如果 它们的半径之和大于它们中心之间的距离,但是 您正在获取半径的差异并检查它是否 中心之间的距离。

此外,您应该在i == j时跳过重叠检查(因为每个圆圈重叠 与自己;你只对不同圈子之间的重叠感兴趣。