Simple GUI applicataion problem
The problem is, regardless of whether or not a circle overlaps, it is always cyan colored.
Here is the code, which is seperated into the main code (CirclePanel), and a GUI loader (Circles)
Circles.java:
import javax.swing.JFrame;
public class Circles
{
//-------------------
// Creates and displays the GUI where the circles where be drawn
//-------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Circles");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
CirclePanel panel = new CirclePanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
CirclePanel.java
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class CirclePanel extends JPanel
{
private final int NUMofCIRCLES = 20;
private final int MAXX = 800;
private final int MAXY = 640;
private final int MAXRADIUS = 65;
double lengthx, lengthy, sumOfRadii;
private Random generator;
private int[][] circleArray = new int[NUMofCIRCLES][4];
//-------------------
// Constructor: sets up the basic characteristics of this panel.
//-------------------
public CirclePanel()
{
setBackground (Color.white);
setPreferredSize (new Dimension(MAXX, MAXY));
generator = new Random();
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);
// Fill 2D array CircleArray with properties of each circle
for (int count = 0; count < NUMofCIRCLES; count++)
{
circleArray[count][0] = generator.nextInt(MAXX - MAXRADIUS); // [0] is x coordinate
circleArray[count][1] = generator.nextInt(MAXY - MAXRADIUS); // [1] is y coordinate
circleArray[count][2] = generator.nextInt(MAXRADIUS); // [2] is length of radius
}
// Draw the circles one at a time
for (int count = 0; count < NUMofCIRCLES; count++)
{
boolean overlaps = false;
// Checks for overlap. If the distance between the center points, sqrt((y-y0)^2 + (x-x0)^2)
// is less than the sum of the radii, than the circle does overlap, and fillcolor is cyan.
for (int count2 = 0; count2 < NUMofCIRCLES; count2++)
{
lengthx = circleArray[count2][0] - circleArray[count][0];
lengthy = circleArray[count2][1] - circleArray[count][1];
sumOfRadii = circleArray[count2][2] + circleArray[count][2];
if (overlaps == false)
{
if (Math.sqrt((lengthx*lengthx) + (lengthy*lengthy)) < sumOfRadii)
{
overlaps = true;
}
}
}
int sidelength = circleArray[count][2]*2;
if (overlaps == true)
page.setColor(Color.cyan);
if (overlaps == false)
page.setColor(Color.black);
page.fillOval (circleArray[count][0], circleArray[count][1], sidelength, sidelength);
}
}
}

