Note that there are some explanatory texts on larger screens.

plurals
  1. POdrawing random circles, storing their coorindates in an array
    primarykey
    data
    text
    <p>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 other circle, 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 circle. Two circles overlap if the distance between their center points is less than the sum of their radii. </p> <p>I am really close, but I just cannot figure out how to use the sqrt formula in order to compare the radii of the circles that overlap and then to redraw that circle in cyan. I've tried to figure this out in two other posts here: <a href="https://stackoverflow.com/questions/4197027/drawing-random-circles-storing-their-coorindates-in-an-array">drawing random circles, storing their coorindates in an array</a> and here: <a href="https://stackoverflow.com/questions/4258036/draw-random-circles-first-storing-the-points-in-an-array">draw random circles, first storing the points in an array</a>. I got some pointers, so can anyone give me specific pointers to figure out how to make my for loop use the Math.sqrt function properly in order to compare the radii and then redraw an overlapping circle in cyan? Thank you very much.</p> <p>UPDATE: I have gotten the Math.sqrt forumla working, but I can't figure out how to structure my for loop in order to only make the circle that overlaps be filled in. I tried to do this using a nested for loop with a boolean in it, but that is making all of the circles fill in. Thank you for your recommendations so far.</p> <p>Here is the code that I have so far:</p> <pre><code> 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&lt;xArray.length; i++){ //random numbers from 1 to 20; xArray[i] = r.nextInt(200) + 1; } for (int i = 0; i&lt;yArray.length; i++){ yArray[i] = r.nextInt(200) + 1; } for (int i = 0; i&lt;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&lt;xArray.length; i++) //this is an iterator that draws the circles and checks for overlapping radii for (int j = 0 ;j&lt;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])) &gt;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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload