绘制随机圆,首先将点存储在数组中 [英] draw random circles, first storing the points in an array
问题描述
这是我要完成的作业: 设计并实现一个绘制圆的程序,并随机确定每个圆的半径和位置.如果一个圆不与其他圆重叠,则以黑色绘制该圆.如果一个圆与一个或多个圆重叠,则用青色绘制.使用数组存储每个圆的表示形式,然后确定每个弯头的颜色.如果两个圆的中心点之间的距离小于其半径之和,则两个圆重叠.
This is what I want to accomplish for homework: Design and implement a program that draws circles, with the radius and location of each circle determined at random. If a circle does not overlap with any othe rcircle, draw that circle in black. If a circle overlaps one or more circles, draw it in cyan. Use an array to store a representation of each circle, then determine the color of each crcle. Two circles overlap if the distance betwen their center points is less than the sum of their radii.
这是我到目前为止的代码:
Here is the code that I have so far:
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 [4]; // radius of each circle
private int[] xArray = new int [4]; //array to store x coordinates of circles
private int[] yArray = new int [4]; //array to store y coordinates of circles
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.white);
setPreferredSize (new Dimension(300, 200));
}
// Draws 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 (math.sqrt((x1-x2)*((x1-x2))-((y1-y2)*(y1-y2));
{//math.sqrt((x1-x2)*(x1-x2)-(y1-y2)*(y1-y2)), go back and read chapter 7
page.fillOval(xArray[i], yArray[i], sizeArray[i], sizeArray[i]);
}
}
}
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);
}
}
在听取了我的建议并尝试逐步进行此工作后,我取得了很大的进步.我被困在项目的最后一部分,该项目是将圆的中心点与半径的总和进行比较,以确定圆是否重叠.我认为可以使用math.sqrt函数进行比较,但是我不确定如何正确实现它.我知道一旦弄清楚如何检查它们是否重叠(布尔值是正确的),就可以在青色中绘制该圆圈.如果有人提出任何意见,我将非常感激,但我知道如果没有,我知道寻求太多帮助是不好的.非常感谢.
After taking the advice that I received and trying to go through this step by step, I made a lot of progress. I am stuck on the last bit of the project, which is to compare the center points of the circles to the sum of their radii to determine if the circles overlap. I think that I can use the math.sqrt function to compare them, but I'm not sure how to implement it correctly. I know that once I figure out how to check, if they are overlapping (a boolean is true) then I will paint that circle in Cyan. If anyone has any input I would be really appreciative, but I understand if not, I know that asking for too much help is not good. Thank you very much.
推荐答案
我不知道如何为圆的半径生成随机数....
I am lost on how to generate random numbers for the radii of the circles....
首先,编写代码以创建一个具有固定位置和半径的圆.
First, write the code to create one circle with a fixed position and radius.
然后扩展为:多次调用randomGenerator.nextInt()
以获得位置和半径的随机值.
Then expand on that: Call randomGenerator.nextInt()
a couple of times to get random values for the position and radius.
下一步:将其放入10次循环
Next step: Put that into a 10-times loop
下一步:运行循环N次,再次从randomGenerator.nextInt()
获得N
Next step: Run the loop N times where you get N from randomGenerator.nextInt()
again
食谱:总是从最简单的例子开始(无论如何我如何画圆?).然后一步一步扩展它.避免我可以立即做所有事情."
Recipe: Always start with the most simple example (how do I draw circles anyway?). Then expand it step by tiny step. Avoid "I can do everything at once."
这篇关于绘制随机圆,首先将点存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!