如何使3个随机圆圈出现并刷新而不重叠?

时间:2016-01-09 01:59:24

标签: java animation overlap

我目前有一个程序随机绘制3个圆圈,然后每秒刷新一次,圆圈出现在不同的位置。关键是不要让它们重叠。这就是我到目前为止所拥有的。但是,直到圆实际重叠为止。当发生这种情况时,它会冻结。如果有人能帮助我,我会非常感激。谢谢! :)

rX = randy.nextInt(width - fudgeX - diameter);
    rY = randy.nextInt(height - fudgeY - diameter);
    Bx = randy.nextInt(width - fudgeX - diameter);
    By = randy.nextInt(height - fudgeY - diameter);
    Gx = randy.nextInt(width - fudgeX - diameter);
    Gy = randy.nextInt(height - fudgeY - diameter);

    g.setColor(Color.red);
    g.fillOval(rX, rY, diameter, diameter);

    BR = distance(rX, rY, Bx, By);

    while(BR < diameter)
    {
        Bx = randy.nextInt(width - fudgeX - diameter);
        By = randy.nextInt(height - fudgeY - diameter);
        distance(rX, rY, Bx, By);
    }

    g.setColor(Color.BLUE);
    g.fillOval(Bx, By, diameter, diameter);



    GR = distance(rX, rY, Gx, Gy);
    GB = distance(Bx, By, Gx, Gy);
    while(GR < diameter || GB < diameter)
    {
        Gx = randy.nextInt(width - fudgeX - diameter);
        Gy = randy.nextInt(height - fudgeY - diameter);

        distance(rX, rY, Gx, Gy);
        distance(Bx, By, Gx, Gy);
    }
    g.setColor(Color.GREEN);
    g.fillOval(Gx, Gy, diameter, diameter);


    repaint();  
}

public double distance(int x1, int y1, int x2, int y2)
{
    int changeX = x1 - x2;
    int changeY = y1 - y2;

    int squareX1 = (int) Math.pow((double)changeX, 2.0);
    int squareY1 = (int) Math.pow((double)changeY, 2.0);

    double d = Math.sqrt(squareX1 + squareY1);

    return d;
}

0 个答案:

没有答案