绘制随机圆,首先将点存储在数组中
这就是我想要完成的家庭作业: 设计并实现一个绘制圆圈的程序,每个圆圈的半径和位置是随机确定的。如果一个圆不与任何其他圆重叠,则将该圆绘制为黑色。如果一个圆与一个或多个圆重叠,则将其绘制为青色。使用数组存储每个圆的表示,然后确定每个圆的颜色。如果两个圆的中心点之间的距离小于它们的半径之和,则两个圆重叠。
这是我到目前为止所拥有的代码:
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 函数来比较它们,但我不确定如何正确实现它。我知道,一旦我弄清楚如何检查它们是否重叠(布尔值为真),那么我将用青色绘制该圆圈。如果有人有任何意见,我将非常感激,但我理解,如果没有,我知道寻求太多帮助是不好的。非常感谢。
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);
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,编写代码来创建一个具有固定位置和半径的圆。
然后对此进行扩展:调用 randomGenerator.nextInt() 几次以获取位置和半径的随机值。
下一步:将其放入 10 次循环中
下一步:运行循环 N 次,再次从 randomGenerator.nextInt() 中获取 N
配方:始终从最简单的示例开始(我该如何做)无论如何画圆圈?)。然后一步一步地扩大。避免“我可以同时做所有事情”。
First, write the code to create one circle with a fixed position and radius.
Then expand on that: Call
randomGenerator.nextInt()
a couple of times to get random values for the position and radius.Next step: Put that into a 10-times loop
Next step: Run the loop N times where you get N from
randomGenerator.nextInt()
againRecipe: 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."
您已经生成了一个随机整数。现在您需要将值存储在数组中。
是的,只需将每个圆圈与所有其他圆圈进行比较即可确定它是否重叠。
You are already generating a random int. Now you need to store the value(s) in your array.
Yes, just compare each circle to all the others to determine if it overlaps.