Reputation: 111
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.
Upvotes: 0
Views: 3494
Reputation: 328850
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.
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()
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."
Upvotes: 4
Reputation: 23639
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.
Upvotes: 1